Thursday, July 14, 2011

เมธอดต่างๆ ของ Javascript ที่ใช้ใน WebPage

เมธอด ของ Javascript
เมธอด ของ Javascript สำหรับ JavaScript นักพัฒนาเว็บไซต์มักจะใช้ Object ต่างๆที่มีอยู่ใน Javascript มาใช้ประกอบกับการทำเว็บไซต์ ซึ่ง JavaScript นั้นมีเมธอดต่างๆดังนี้
document.write(“text”) 
เมธอดนี้ จะใช้แสดงข้อความต่างๆในหน้าเว็บเพจ
docment.clear()
เมธอดนี้ใช้สำหรับ ลบหน้าเว็บเพจทั้งหมด

window.open()
เมธอดนี้ใช้สำหรับเปิด Browser ใหม่
history.go()
เมธอดนี้ใช้สำหรับ ระบุหน้าที่ต้องการ สามารถระบุจากประวัติการเปิดการเปิดหน้าเว็บเพจที่ผ่านมา

document.bgColor=”code color”

เมธอดนี้ใช้สำหรับกำหนดสีพื้นของเว็บเพจ
document.fgColor=”code color”
เมธอดนี้ใช้สำหรับกำหนดสีของตัวอักษรบนเว็บเพจ
document.title
เมธอดนี้ใช้สำหรับ แสดงชื่อของเว็บเพจ
document.lastModified
เมธอดนี้สำหรับ แสดงวันที่ มีการปรับแต่งหน้าเว็บเพจครั้งสุดท้าย

Wednesday, July 13, 2011

เป็นตัวอักษรวิ่งอยู่ในหน้าเว็บ ในช่องที่กำหนด

<html>
<title>www.thaiall.com</title>
<script language="JavaScript">
<!--
var timerID = null;
var timerRunning = false;
var id,pause=0,position=0;
function ticker() {
var i,k,msg="TEST";
k=(60/msg.length)+1;
for(i=0;i<=k;i++) msg+=" "+msg;
document.form2.ticker.value=msg.substring(position,position+100);
if(position++==300) position=0;
id=setTimeout("ticker()",2000/5);
}
//-->
</script>
<body onload="ticker()">
<form name="form2">
<input type="text" name="ticker" size="70" value="">
</form>
</body></html>

Tuesday, July 12, 2011

javaScript Array

<html>
<body>

<script type="text/javascript">
var i;
var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";

for (i=0;i<mycars.length;i++)
{
document.write(mycars[i] + "<br />");
}
</script>

</body>
</html>

HTML Image

<html>
<body>

<img src="angry.gif" alt="Angry face" width="32" height="32" />

</body>
</html>

HTML Links

Example

<a href="http://www.w3schools.com/" target="_blank">Visit W3Schools!</a>

HTML Styles

HTML Style Example - Background Color

The background-color property defines the background color for an element:

Example

<html>

<body style="background-color:yellow;">
<h2 style="background-color:red;">This is a heading</h2>
<p style="background-color:green;">This is a paragraph.</p>
</body>

</html>
The background-color property makes the "old" bgcolor attribute obsolete.


HTML Style Example - Font, Color and Size

The font-family, color, and font-size properties defines the font, color, and size of the text in an element:

Example

<html>

<body>
<h1 style="font-family:verdana;">A heading</h1>
<p style="font-family:arial;color:red;font-size:20px;">A paragraph.</p>
</body>

</html>

The font-family, color, and font-size properties make the old <font> tag obsolete.

HTML Style Example - Text Alignment

The text-align property specifies the horizontal alignment of text in an element:

Example

<html>

<body>
<h1 style="text-align:center;">Center-aligned heading</h1>
<p>This is a paragraph.</p>
</body>

</html

HTML Fonts

Example

<p>
<font size="5" face="arial" color="red">
This paragraph is in Arial, size 5, and in red text color.
</font>
</p>

<p>
<font size="3" face="verdana" color="blue">
This paragraph is in Arial, size 5, and in red text color.
</font>
</p>

HTML Formatting

HTML Text Formatting Tags

TagDescription
<b>Defines bold text
<big>Defines big text
<em>Defines emphasized text 
<i>Defines italic text
<small>Defines small text
<strong>Defines strong text
<sub>Defines subscripted text
<sup>Defines superscripted text
<ins>Defines inserted text
<del>Defines deleted text

HTML "Computer Output" Tags

TagDescription
<code>Defines computer code text
<kbd>Defines keyboard text 
<samp>Defines sample computer code
<tt>Defines teletype text
<var>Defines a variable
<pre>Defines preformatted text

HTML Citations, Quotations, and Definition Tags

TagDescription
<abbr>Defines an abbreviation
<acronym>Defines an acronym
<address>Defines contact information for the author/owner of a document
<bdo>Defines the text direction
<blockquote>Defines a long quotation
<q>Defines a short quotation
<cite>Defines a citation
<dfn>Defines a definition term

HTML Attributes

Example

<a href="http://www.w3schools.com">This is a link</a>

Tuesday, July 5, 2011

โปรแกรมการหา Factorial

#include <stdio.h>
void main()
{
int count;
int fac;
int ans =1;

printf("Please Enter number: ");
scanf("%d",&fac);

count = fac;
while(count>0)
{
ans = ans*count;
count--;
}
printf("%d! = %d\n",fac,ans);
}



Monday, July 4, 2011

ตอนที่ 3 : การรับค่า ส่งค่า จาก function

. . . . . . . .มาแล้วๆ หายไปนานโทษที แหะ แหะ เอาละคราวนี้เป็นการตั้งเงื่อนไขให้กับโปรแกรม และการผ่านค่าไปยัง function ดูได้จากเวลาเราจะให้โปรแกรมทำอะไร เราก็ต้องส่งค่าไปยัง function ที่ต้องการ แบบนี้
#include <stdio.h>#include <conio.h>
int main( )
{
printf( "Sawasdee\n" ); //<-- ดูตรงนี้
return 0;
}
Sawasdee
. . . . . . . .จากตัวอย่างแจกแจงได้ออกเป็น
printf คือ function
"Sawasdee" คือ ข้อความที่ส่งไปให้ function ทำงานนั่นเอง
. . . . . . . .มาดูอีกตัวอย่าง การส่งค่าให้กับ function ที่เราเขียนขึ้นเอง
#include <stdio.h>#include <conio.h>
void function1( char *msg ) //<-------- รับค่าเป็นข้อความ จะอธิบายให้ทีหลังเรื่อง char * นะครับ
{
printf( "%s", msg ); //<------ รับค่าที่ส่งมา ไปให้ function printf แสดงข้อความที่ส่งมา
}
int function2( int param1, int param2 ) //<-------- รับค่าเป็น Integer
{
printf( "%i %i", param1, param2 ); //<------ รับค่าที่ส่งมา ไปให้ function printf แสดงค่าของตัวแปร param1 และ param2
return 0; //<--- ต้องส่งค่ากลับเพราะไม่ใช่ function แบบ void
}
int main( )
{
printf( "Sawasdee\n" );
function1( " ha ha ha" ); //<-- ดูตรงนี้
function2( 10, 20 );
return 0;
}
Sawasdeeha ha ha
10 20
. . . . . . . .จากตัวอย่างข้างบนเป็นการส่งค่าไปให้ function และค่าที่เราส่งไปนั้นเราเรียกว่า พารามิเตอร์ เราสามารส่งค่า พารามิเตอร์ ได้หลายตัวและก็ได้หลากหลายรูปแบบ ลองดูตัวอย่างนะครับ เป็นการส่งค่าไปให้ function แบบผสม
#include <stdio.h>#include <conio.h>
void function1int param1, int param2, long param2, char *param4//<-------- รับค่าเป็น Integer, Long, char*
{
printf( "%i %i %i %s \n", param1, param2, param3, param4); //<------ รับค่าที่ส่งมา ไปให้ function printf
return 0; //<--- ต้องส่งค่ากลับเพราะไม่ใช่ function แบบ void
}
int main( )
{
printf( "Sawasdee\n" );
function1( 10, 20, 100, " ha ha ha" ); //<-- ดูตรงนี้
return 0;
}
Sawasdee10 20 100 ha ha ha
. . . . . . . .ต่อไปเป็นการส่งค่าคืนกลับ สังเกตุการกำหนดรูปแบบ function นะครับ จะเห็นได้ว่ามีทั้งแบบ void และ int ถ้าเป็น void เราไม่ต้องส่งค่ากลับ แต่ถ้าเป็น int เราต้องส่งค่ากลับและการรับค่าที่ส่งกลับเราทำได้แบบนี้
#include <stdio.h>#include <conio.h>
int function1int param1, int param2 ) //<------ ต้องกำหนดรูปแบบ function ไม่ให้เป็นรูปแบบ void
{
return param1 + param2; //<--- ต้องส่งค่ากลับเพราะไม่ใช่ function แบบ void
}
int main( )
{
int i = function1( 10, 20 ); //<------ เรียก fuction1 แล้วส่งค่าคืนให้กับ ตัวแปร i
printf( "Value in %i", i ); //<----- พิมพ์ค่าที่อยู่ใน i
return 0;
}
Value in 30
. . . . . . . .การส่งค่ากลับเราต้องใช้คำสั่ง return นะครับ จะส่งค่าแบบไหนก็ใช้ return ลองเขียน function มาเยอะๆ แล้วมาผสมกับแบบนี้ดูนะครับ จะสามารถพลิกแพงได้หลายตลบเลยทีเดียว ดูตัวอย่างข้างล่างนะครับ
#include <stdio.h>#include <conio.h>
int function1int param1, int param2 )
{
return param1 + param2; //<--- ต้องส่งค่ากลับเพราะไม่ใช่ function แบบ void
}
int function2( int param1, int param2 )
{
return param1 * param2; //<----- สั่งให้ทำการคูณตัวแปรที่ส่งมาและส่งกลับ
}
int main( )
{
int i = function1( 10, 20 ) + function2(10,20); //<----- สังเกตดี ดี
printf( "Value in %i", i ); //<----- พิมพ์ค่าที่อยู่ใน i
return 0;
}
Value in 230
. . . . . . . .การทำงานของตัวอย่างข้างบนคือ เรียก function1 มาทำการบวกค่าที่อยู่ในตัวแปร param2 และ param2 ต่อไปเรียก function2 มาทำการคูณค่าที่อยู่ในตัวแปร param1 และ param2 และส่งค่ากลับไปยังตัวแปร i แล้วก็พิมพ์ออกมา ง่ายๆ ครับลองดูหลายๆรอบ ต่อไป ถ้าหากว่าเราต้องการส่งค่ามากกว่า 1 การใช้ return ส่งได้เพียงค่าเดียวแล้วเราจะทำยังงัย เอ้อ..................hmmm....... งั้นดูนี่
#include <stdio.h>#include <conio.h>
int function1int param1, int *param2 )
{
*param2 = param1; //<------ ให้ค่า param1 แก่ param2 สังเกตุเครื่องหมายดอกจันท์นะครับ
return param1;
}
int main( )
{
int i;
int param2; //<--- กำหนดตัวแปรเพื่อที่จะรับค่าที่จะส่งกลับมา
i = function1( 10, &param2 ); //<-------- ดูที่ param2 นะครับ
i = function1( 10, 20 ); //<-------- ห้าม !! ทำแบบนี้
printf( "%i %i\n", i, param2 ); //<----- พิมพ์ค่าที่อยู่ใน i และ param2
return 0;
}
1010
. . . . . . . .เริ่มมีตัวประหลาดเข้ามาแล้วทีนี้ ฮ่า ฮ่า ฮ่า ไม่ยากครับ มันเป็นรูปแบบเฉยๆ ดูที่ function1 นะครับ
int function1int param1int *param2 )
. . . . . . . .สังเกตที่ param2 จะมีเครื่องหมายดอกจันท์ เพื่อที่จะบอกว่าตัวแปรนั้นส่งค่ากลับได้ และการใช้ตัวแปร param2 ต้องมีเครื่องหมายดอกจันทน์นำหน้าเสมอ แล้วการส่งค่าไปยัง function1 ต้องมีเครื่องหมาย & นำหน้าตัวแปรเสมอ อย่าส่งตัวเลขไปละ ต้องสร้างตัวแปรมารองรับครับ ดูอีกตัวอย่าง
#include <stdio.h>#include <conio.h>
int function1int param1, int *param2 )
{
*param2 = param1 + (*param2); //<------ ให้ค่า param1 + param2
return param1;
}
int main( )
{
int i;
int param2 = 20; //<--- กำหนดตัวแปรเพื่อที่จะรับค่าที่จะส่งกลับมา และให้ค่าเริ่มต้นแก่ตัวแปร
i = function1( 10, &param2 ); //<--- ต้องมีเครื่องหมาย & นำเหน้าเสมอ
printf( "%i %i\n", i, param2 );
return 0;
}
1030
. . . . . . . .คราวนี้มีการให้ค่าแก่ตัวแปร param2 ก่อนแล้วนำไปทำการบวกกันที่ function1 เราต้องใส้เครื่องหมายวงเล็กครอบตัวแปร param2 นะครับ เพราะถ้าเขียนแบบนี้
*param2 = param1 + *param2;
. . . . . . . .เดี๋ยวเราจะงงถ้าไม่มีวงเล็บ ก็ให้รู้ไว้ว่าถ้าเราจะใช้ตัวแปรแบบ param2 ก็ต้องมีเครื่องหมายวงเล็บ แต่ถ้าจะให้ค่า ก็ไม่ต้องมีเครื่องหมายวงเล็กนะครับ แบบนี้
*param2 คือ การให้ค่าแก่ตัวแปร
(*param2) คือ การใช้ตัวแปร
. . . . . . . .และเราสามารถส่งค่ากลับมาได้หลายๆตัวเช่นกัน ลองเขียนดูนะครับ
#include <stdio.h>#include <conio.h>
int function1int param1, int *param2, int *param3 )
{
*param2 = param1 + (*param2); //<------ ให้ค่า param1 + param2
*param3 = 30; //<------ ให้ค่า param3
return param1;
}
int main( )
{
int i;
int param2 = 20; //<--- กำหนดตัวแปรเพื่อที่จะรับค่าที่จะส่งกลับมา และให้ค่าเริ่มต้นแก่ตัวแปร
int param3; //<--- กำหนดตัวแปรเพื่อที่จะรับค่าที่จะส่งกลับมา ไม่ได้กำหนดค่าอะไร
i = function1( 10, &param2, &param3 ); //<--- ต้องมีเครื่องหมาย & นำเหน้าเสมอ
printf( "%i %i %i\n", i, param2, param3 );
return 0;
}
1030
30
. . . . . . . .จะเห็นได้ว่าเราสามารถเขียน function ไปคำนวณและส่งค่ากลับมาได้หลายตัวเลยทีเดียว ทำให้การใช้งานยิ่งสะดวกขึ้น ลองเขียน function มาเยอะๆ นะครับ หลายๆ รูปแบบ

สรุป วันนี้ได้อะไรหว่า ???

void function1( char *msg ) //<-------- รับค่าเป็นข้อความ จะอธิบายให้ทีหลังเรื่อง char * นะครับint function2( int param1, int param2 ) //<-------- รับค่าเป็น Integer
void function1int param1, int param2, long param2, char *param4//<-------- รับค่าเป็น Integer, Long, char*
return param1 + param2; //<--- ต้องส่งค่ากลับเพราะไม่ใช่ function แบบ void
int i = function1( 10, 20 ); //<------ เรียก fuction1 แล้วส่งค่าคืนให้กับ ตัวแปร i
int function1int param1, int *param2 ) //<--- ดูที่ param2
int function1int param1, int *param2, int *param3 )
*param2 = param1; //<------ ให้ค่า param1 แก่ param2 สังเกตุเครื่องหมายดอกจันท์นะครับ
i = function1( 10, 20 ); //<-------- ห้าม !! ทำแบบนี้
*param2 = param1 + (*param2); //<------ ให้ค่า param1 + param2
*param3 = 30; //<------ ให้ค่า param3
i = function1( 10, &param2, &param3 ); //<--- ต้องมีเครื่องหมาย & นำเหน้าเสมอ

ตอนที่ 6: โครงสร้างข้อมูล

. . . . . . . .วันนี้เป็นเรื่องโครงสร้างช้อมูล ถ้าเราเคยใช้ระบบ record ของฐานข้อมูล DBASE, Foxpro หรือ ฐานข้อมูลอื่นๆ จะเห็นโครงสร้างข้อมูล จะมีรายละเอียดย่อยที่เราเรียกว่า ฟิลด์ และการใช้งานในลักษณะนั้น สามารถทำได้แบบนี้
#include <stdio.h>#include <conio.h>
typedef struct TRecord //<------ วิธีกำหนดโครงสร้าง
{ //<--- ขอบเขตของโครงสร้าง
char data[80]; //<----- รายการย่อยเรียกว่า ฟิลด์ชื่อ data
char name[80]; //<----- รายการย่อยเรียกว่า ฟิลด์ชื่อ name
}; //<--- สิ้นสุดขอบเขตของโครงสร้าง
int main( )
{
struct TRecord rec1; //<------ ตัวแปรโครงสร้างของ TRecord ชื่อ rec1
strcpy( rec1.data, "Sawasdee" ); //<----- copy ข้อความให้ฟิลด์ data ของ rec1
printf( "%s\n", rec1.data ); //<----- พิมพ์ข้อมูล
return 0;
}
Sawasdee
. . . . . . . .จากตัวอย่างข้างบน วิธีการกำหนดโครงสร้างให้ทำแบบนี้
typedef struct TRecord //<------ วิธีกำหนดโครงสร้าง
{
//...... ข้างในนี้คือรายการย่อยเรียกว่า ฟิลด์
};
การกำหนดตัวแปรโครงสร้างให้ทำแบบนี้
struct TRecord rec1; //<------ ตัวแปรโครงสร้างของ TRecord ชื่อ rec1
การใช้งานฟิลด์ในโครงสร้างให้ทำแบบนี้
rec1.data //<------- ให้ใส่จุดไช่ปลาแล้วตามด้วยชื่อฟิลด์
ดูแล้วไม่ยากเลยใช่ไหม กลับไปดูใหม่ได้นะครับ ต่อไปเป็นโครงสร้างแบบซ้อน
#include <stdio.h>#include <conio.h>
typedef struct TSubRecord
{
char data[80]; //<----- รายการย่อยเรียกว่า ฟิลด์ชื่อ data
};
typedef struct TRecord //<------ วิธีกำหนดโครงสร้าง
{ //<--- ขอบเขตของโครงสร้าง
char data[80]; //<----- รายการย่อยเรียกว่า ฟิลด์ชื่อ data
struct TSubRecord sub1; //<----- ตัวแปรโครงสร้างซ้อน
}; //<--- สิ้นสุดขอบเขตของโครงสร้าง
int main( )
{
struct TRecord rec1; //<------ ตัวแปรโครงสร้างของ TRecord ชื่อ rec1
strcpy( rec1.data, "Sawasdee" ); //<----- copy ข้อความให้ฟิลด์ data ของ rec1
strcpy( rec1.sub1.data, "Sawasdee2" ); //<----- copy ข้อความให้ฟิลด์ data ของ sub1
printf( "%s\n", rec1.data ); //<----- พิมพ์ข้อมูล
printf( "%s\n", rec1.sub1.data ); //<----- พิมพ์ข้อมูล
return 0;
}
SawasdeeSawasdee2
. . . . . . . .จากตัวอย่างข้างบน เป็นการกำหนดฟิลด์แบบโครงสร้าง แต่การเข้าถึงฟิลด์ก็เหมือนเดิม คือ ต้องมีจุดไข่ปลาขั้นแล้วตามด้วยชื่อฟิลด์ที่จะใช้งานการใช้งานฟิลด์ในโครงสร้างแรก
rec1.data //<------- ให้ใส่จุดไช่ปลาแล้วตามด้วยชื่อฟิลด์
การใช้งานฟิลด์ในโครงสร้างที่สอง
rec1.sub1.data //<------- ให้ใส่จุดไช่ปลาแล้วตามด้วยชื่อฟิลด์
ไม่ยากอะไรใช่ไหม ลองเพิ่มฟิลด์สิครับ ไม่จำกัดรูปแบบ แต่อย่าลืมการอ้างถึงต้องมีจุดไข่ปลาเสมอ ลองดูอีกแบบนะครับ เป็นการกำหนดฟิลด์แบบต่างๆในโครงสร้าง
#include <stdio.h>#include <conio.h>
typedef struct TSubRecord
{
char data[80]; //<----- รายการย่อยเรียกว่า ฟิลด์ชื่อ data
};
typedef struct TRecord //<------ วิธีกำหนดโครงสร้าง
{ //<--- ขอบเขตของโครงสร้าง
char data[80]; //<----- รายการย่อยเรียกว่า ฟิลด์ชื่อ data
int value1; //<------ กำหนดแบบ Integer
long value2; //<--- กำหนดแบบ long
struct TSubRecord sub1; //<----- ตัวแปรโครงสร้างซ้อน
}; //<--- สิ้นสุดขอบเขตของโครงสร้าง
int main( )
{
struct TRecord rec1;
strcpy( rec1.data, "Sawasdee" );
rec1.value1 = 100;
rec1.value2 = 200;
strcpy( rec1.sub1.data, "Sawasdee2" ); //<----- copy ข้อความให้ฟิลด์ data ของ sub1
printf( "%s\n", rec1.data );
printf( "%i\n", rec1.value1 );
printf( "%i\n", rec1.value2 );
printf( "%s\n", rec1.sub1.data );
return 0;
}
Sawasdee100
200
Sawasdee2

วันนี้ได้อะไรหว่า ???

typedef struct TSubRecord{
char data[80]; //<----- รายการย่อยเรียกว่า ฟิลด์ชื่อ data
};
typedef struct TRecord //<------ วิธีกำหนดโครงสร้าง
{ //<--- ขอบเขตของโครงสร้าง
char data[80]; //<----- รายการย่อยเรียกว่า ฟิลด์ชื่อ data
int value1; //<------ กำหนดแบบ Integer
long value2; //<--- กำหนดแบบ long
struct TSubRecord sub1; //<----- ตัวแปรโครงสร้างซ้อน
}; //<--- สิ้นสุดขอบเขตของโครงสร้าง
strcpy( rec1.data, "Sawasdee" );
rec1.value1 = 100;
rec1.value2 = 200;
strcpy( rec1.sub1.data, "Sawasdee2" ); //<----- copy ข้อความให้ฟิลด์ data ของ sub1