<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>jQuery, Ajax, PHP, JSON, XML, Web Technologies &#187; Events</title>
	<atom:link href="http://blog.chonla.com/category/events/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.chonla.com</link>
	<description>คุยกันเรื่อง jQuery, Ajax, PHP, JSON, XML และ Web Technologies เป็นภาษาไทยกันดีกว่า</description>
	<lastBuildDate>Thu, 02 Sep 2010 18:43:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>jQuery $.ready() กับ window.onload ความเหมือนที่แตกต่าง</title>
		<link>http://blog.chonla.com/2008/11/jquery-ready-%e0%b8%81%e0%b8%b1%e0%b8%9a-windowonload-%e0%b8%84%e0%b8%a7%e0%b8%b2%e0%b8%a1%e0%b9%80%e0%b8%ab%e0%b8%a1%e0%b8%b7%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88%e0%b9%81%e0%b8%95/</link>
		<comments>http://blog.chonla.com/2008/11/jquery-ready-%e0%b8%81%e0%b8%b1%e0%b8%9a-windowonload-%e0%b8%84%e0%b8%a7%e0%b8%b2%e0%b8%a1%e0%b9%80%e0%b8%ab%e0%b8%a1%e0%b8%b7%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88%e0%b9%81%e0%b8%95/#comments</comments>
		<pubDate>Thu, 06 Nov 2008 04:09:58 +0000</pubDate>
		<dc:creator>chonla</dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Event Binding]]></category>
		<category><![CDATA[ready]]></category>
		<category><![CDATA[window.onload]]></category>

		<guid isPermaLink="false">http://blog.chonla.com/?p=115</guid>
		<description><![CDATA[คงมีหลายคนไม่น้อยที่เคยใช้การสั่งให้ฟังก์ชั่นทำงานตอน document โหลดเสร็จ ไม่ว่าจะเป็นการใช้ onload event ใน &#60;body&#62; หรือจะเป็นการกำหนดค่าใน window.onload event ของ javascript เมื่อมองถึง jQuery แล้ว jQuery มี function ที่ทดแทน (ที่ดีกว่า ผมเชื่ออย่างนั้น) คือ ready() ซึ่งทำงานใกล้เคียงกัน การใช้ $.ready() หรือ $() หรือ $(document).ready() จะเกิดขึ้นเมื่อ document ถูกโหลดและ parse เป็น DOM สำเร็จ เรียกง่าย ๆ ว่า ทำเมื่อ document ถูกโหลดแล้วนั่นเอง เราสามารถใช้ $() แทนการเขียนรูปแบบอื่น ๆ ได้ เพื่อให้สั้นและกระชับ และฟังก์ชั่นนี้เองที่เราจะเอามาแทน window.onload ข้อแตกต่างระหว่าง $() และ [...]]]></description>
			<content:encoded><![CDATA[<p>คงมีหลายคนไม่น้อยที่เคยใช้การสั่งให้ฟังก์ชั่นทำงานตอน document โหลดเสร็จ ไม่ว่าจะเป็นการใช้ onload event ใน &lt;body&gt; หรือจะเป็นการกำหนดค่าใน window.onload event ของ javascript</p>
<p>เมื่อมองถึง jQuery แล้ว jQuery มี function ที่ทดแทน (ที่ดีกว่า ผมเชื่ออย่างนั้น) คือ ready() ซึ่งทำงานใกล้เคียงกัน</p>
<p><span id="more-115"></span></p>
<p>การใช้ $.ready() หรือ $() หรือ $(document).ready() จะเกิดขึ้นเมื่อ document ถูกโหลดและ parse เป็น DOM สำเร็จ เรียกง่าย ๆ ว่า ทำเมื่อ document ถูกโหลดแล้วนั่นเอง เราสามารถใช้ $() แทนการเขียนรูปแบบอื่น ๆ ได้ เพื่อให้สั้นและกระชับ และฟังก์ชั่นนี้เองที่เราจะเอามาแทน window.onload</p>
<p>ข้อแตกต่างระหว่าง $() และ window.onload อย่างหนึ่งที่เห็นได้ชัดคือ รูปแบบการทำงานของฟังก์ชั่นที่เอามาผูกด้วย</p>
<p>ผมหมายถึงถ้ามีการเรียก window.onload = &lt;ชื่อฟังก์ชั่น&gt; หลายครั้ง จะถือว่าการ bind ครั้งสุดท้าย เป็นฟังก์ชั่นที่จะถูกทำงานเมื่อ document ถูกโหลดแล้ว ในขณะที่ $(&lt;ชื่อฟังก์ชั่น&gt;) ถ้าถูกเรียกหลายครั้ง จะเป็นการสั่งให้ทำงานตามลำดับการเรียก หลังจากที่ document ถูกโหลดเสร็จ</p>
<p>และเพื่อให้เห็นภาพได้ชัดเจนขึ้น ลองไปดูได้ที่ <a title="LIVE DEMO : jQuery Ready &amp; Window.Onload Sample" href="http://sandbox.chonla.com/jqueryready" target="_blank">LIVE DEMO</a> ได้เลยครับ</p>
<p>ข้อห้ามอย่างหนึ่งคือในการใช้ $() คือ ห้ามมีการกำหนด window.onload หรือ onload ใน &lt;body&gt; อยู่ด้วยกันกับ $() ไม่งั้นแล้ว ฟังก์ชั่นที่ผูกกับ $() จะไม่ทำงานครับ</p>
<p>สิ่งที่สำคัญอีกเรื่อง ซึ่งผมเคยพูดไปในบทความก่อน ๆ แล้ว คือ การเรียก element ที่ยังไม่ถูกโหลด หรือยังไม่มีอยู่จริงใน DOM ใน javascript การเรียกใช้ element นั้น ๆ ใน window.onload หรือ $() จะสามารถแก้ปัญหานี้ได้ครับ</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://blog.chonla.com/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://blog.chonla.com/2008/11/jquery-ready-%e0%b8%81%e0%b8%b1%e0%b8%9a-windowonload-%e0%b8%84%e0%b8%a7%e0%b8%b2%e0%b8%a1%e0%b9%80%e0%b8%ab%e0%b8%a1%e0%b8%b7%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88%e0%b9%81%e0%b8%95/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Event Binding อย่างไรให้ใช้ได้จริง</title>
		<link>http://blog.chonla.com/2008/10/event-binding-%e0%b8%ad%e0%b8%a2%e0%b9%88%e0%b8%b2%e0%b8%87%e0%b9%84%e0%b8%a3%e0%b9%83%e0%b8%ab%e0%b9%89%e0%b9%83%e0%b8%8a%e0%b9%89%e0%b9%84%e0%b8%94%e0%b9%89%e0%b8%88%e0%b8%a3%e0%b8%b4%e0%b8%87/</link>
		<comments>http://blog.chonla.com/2008/10/event-binding-%e0%b8%ad%e0%b8%a2%e0%b9%88%e0%b8%b2%e0%b8%87%e0%b9%84%e0%b8%a3%e0%b9%83%e0%b8%ab%e0%b9%89%e0%b9%83%e0%b8%8a%e0%b9%89%e0%b9%84%e0%b8%94%e0%b9%89%e0%b8%88%e0%b8%a3%e0%b8%b4%e0%b8%87/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 10:01:51 +0000</pubDate>
		<dc:creator>chonla</dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Event]]></category>
		<category><![CDATA[Event Binding]]></category>
		<category><![CDATA[Run-Time]]></category>

		<guid isPermaLink="false">http://blog.chonla.com/?p=63</guid>
		<description><![CDATA[ผมขออนญาตนำคำถามจากเมลของสมาชิก jQuery [THAI] ท่านหนึ่งมาเล่าให้ฟังเป็นกรณีศึกษานะครับ เนื้อหาของจดหมายพอสรุปได้คร่าว ๆ คือ ใน code มีการ bind event ไว้ และมีการสร้าง element ตอน runtime ในลักษณะนี้ (ผมไม่ได้ใช้ code ที่สมาชิกส่งมาให้ แต่เขียน code ขึ้นใหม่ เพื่อให้ดูง่ายขึ้นนะครับ) ในส่วนของการ binding คือ $("input[type=button]").click(function(){ alert("Hello world"); });    //  (1) และในส่วนของการสร้าง element ตอน runtime คือ $("#container").html("&#60;input type='button' value='Click me'&#62;");    // (2) ปัญหาของกรณีนี้คือ การวางลำดับคำสั่งผิดครับ ลองนึกดูว่า ถ้าเรามีการเรียก code ส่วนที่ (1) ก่อน แล้วค่อยสร้าง element [...]]]></description>
			<content:encoded><![CDATA[<p>ผมขออนญาตนำคำถามจากเมลของสมาชิก <a title="jQuery [THAI]" href="http://groups.google.co.th/group/jquery-thai" target="_blank">jQuery [THAI]</a> ท่านหนึ่งมาเล่าให้ฟังเป็นกรณีศึกษานะครับ เนื้อหาของจดหมายพอสรุปได้คร่าว ๆ คือ ใน code มีการ bind event ไว้ และมีการสร้าง element ตอน runtime ในลักษณะนี้ (ผมไม่ได้ใช้ code ที่สมาชิกส่งมาให้ แต่เขียน code ขึ้นใหม่ เพื่อให้ดูง่ายขึ้นนะครับ)<br />
<span id="more-63"></span><br />
ในส่วนของการ binding คือ</p>
<pre name="code" class="js">
$("input[type=button]").click(function(){ alert("Hello world"); });    //  (1)
</pre>
<p>และในส่วนของการสร้าง element ตอน runtime คือ</p>
<pre name="code" class="js">
$("#container").html("&lt;input type='button' value='Click me'&gt;");    // (2)
</pre>
<p>ปัญหาของกรณีนี้คือ การวางลำดับคำสั่งผิดครับ ลองนึกดูว่า ถ้าเรามีการเรียก code ส่วนที่ (1) ก่อน แล้วค่อยสร้าง element ในส่วน (2) สิ่งที่เกิดจาก (1) คือ มีการ bind event click ให้กับ $(&#8220;input[type=button]&#8220;) ไปแล้วครับ ในขณะที่ element นั้นยังไม่มีอยู่จริงในเว็บ ทำให้ปุ่มที่สร้างขึ้นมาใหม่ไม่ถูก bind ไปด้วยครับ</p>
<p>ปัญหานี้ทำให้ผมนึกถึงอีกกรณีศึกษาหนึ่งครับ ลองดู code ตามนี้ครับ</p>
<pre name="code" class="html">
&lt;!doctype html public "-//w3c//dtd html 4.0 transitional//en"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt; sample &lt;/title&gt;
&lt;script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
$("input[type=button]").click(function(){alert("Hello world");})
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;input type="button" value="click me"&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>ดูเผิน ๆ อาจจะไม่เห็นอะไรผิดปกติ แต่พิจารณาดูดี ๆ แล้ว คำสั่ง $(&#8220;input[type=button]&#8220;).click(function(){alert(&#8220;Hello world&#8221;);}) มันถูกสั่งให้ทำตั้งแต่โหลดหน้า page ยังไม่เสร็จ แน่นอน มันก็จะยังไม่รู้จักกับ &lt;input type=&#8221;button&#8221; value=&#8221;click me&#8221;&gt; เลยครับ ทำให้การ bind event ในลักษณะนี้ไม่เป็นผลเช่นกัน</p>
<p>การ bind event จะต้องเกิดหลังจากที่ element นั้นมีอยู่แล้ว ถึงจะใช้งานได้ครับ ในกรณีศึกษาแรก ผมได้แนะนำให้ไปเรียก binding หลังจากที่สร้าง element แืทน ส่วนกรณีหลัง ผมแนะนำให้ไปไว้ใน $.ready() ครับ</p>
<p>ลองไปดูตัวอย่างได้ใน <a title="LIVE DEMO : Event Binding" href="http://sandbox.chonla.com/eventbind" target="_blank">LIVE DEMO</a> ครับ</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://blog.chonla.com/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://blog.chonla.com/2008/10/event-binding-%e0%b8%ad%e0%b8%a2%e0%b9%88%e0%b8%b2%e0%b8%87%e0%b9%84%e0%b8%a3%e0%b9%83%e0%b8%ab%e0%b9%89%e0%b9%83%e0%b8%8a%e0%b9%89%e0%b9%84%e0%b8%94%e0%b9%89%e0%b8%88%e0%b8%a3%e0%b8%b4%e0%b8%87/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
