<?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>Vivian Crap &#187; Javascript</title>
	<atom:link href="http://www.lingihuang.com/viviancrap/archives/category/web-development/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lingihuang.com/viviancrap</link>
	<description>trust no one</description>
	<lastBuildDate>Sun, 11 Apr 2010 15:16:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The XMLHttpRequest Object</title>
		<link>http://www.lingihuang.com/viviancrap/archives/the-xmlhttprequest-object/</link>
		<comments>http://www.lingihuang.com/viviancrap/archives/the-xmlhttprequest-object/#comments</comments>
		<pubDate>Sat, 08 Aug 2009 04:54:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.lingihuang.com/viviancrap/?p=262</guid>
		<description><![CDATA[




What is the XMLHttpRequest object?





Creating an XMLHttpRequest object.
Specifying and submitting your HTTP request to a web server.
Synchronously or asynchronously retrieving the server&#8217;s response.



Propertie Name
Description


onreadystatechange
An event handler for an event that fires at every state change.


readyState

          					The readyState property defines the current state of the XMLHttpRequest object.

								State [...]]]></description>
			<content:encoded><![CDATA[<div class="entry-content">
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">What is the XMLHttpRequest object?</div>
</p></div>
</p></div>
</p></div>
</p></div>
<ul class="entry-list">
<li>Creating an XMLHttpRequest object.</li>
<li>Specifying and submitting your HTTP request to a web server.</li>
<li>Synchronously or asynchronously retrieving the server&#8217;s response.</li>
</ul>
<div class="entry-table">
<div class="table-heading-row">
<div class="property-col">Propertie Name</div>
<div class="description-col">Description</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:36px;">onreadystatechange</div>
<div class="description-col" style="height:36px;">An event handler for an event that fires at every state change.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:370px;">readyState</div>
<div class="description-col" style="height:370px;">
          					The readyState property defines the current state of the XMLHttpRequest object.</p>
<p>
								<strong>State Description</strong><br />
								0: The request is not initialized.<br />
								1: The request has been set up.<br />
								2: The request has been sent.<br />
								3: The request is in process.<br />
								4: The request is completed.
          					</p>
<p>
          						<strong>readyState = 0</strong> after you have created the XMLHttpRequest object, but before you have called the open() method.<br />
          						<strong>readyState = 1</strong> after you have called the open() method, but before you have called send().<br />
          						<strong>readyState = 2</strong> after you have called the send() method.<br />
          						<strong>readyState = 3</strong> after browser has established a communication with the server, but before the server has completed the response.<br />
          						<strong>readyState = 4</strong> after the request has been completed, and the response data have been completely received from the server.
          					</p>
</p></div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:18px;">responseText</div>
<div class="description-col" style="height:18px;">Returns the response as a string.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:140px;">responseXML</div>
<div class="description-col" style="height:140px;">Returns the response as XML, which can be examined and parsed using W3C DOM node tree methods and properties. This property returns an XML document object, when the Content-Type header specifies the MIME(media) type as text/xml, application/xml, or ends in +xml. If the Content-Type header does not contain one of these media types, the responseXML value is null.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:36px;">status</div>
<div class="description-col" style="height:36px;">Returns the status as a number (e.g. 404 for &#8220;Not Found&#8221; and 200 for &#8220;OK&#8221;).</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:36px;">statusText</div>
<div class="description-col" style="height:36px;">Returns the status as a string (e.g. &#8220;Not Found&#8221; or &#8220;OK&#8221;).</div>
</p></div>
</p></div>
<div class="entry-table">
<div class="table-heading-row">
<div class="property-col">Method Name</div>
<div class="description-col">Description</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:18px;">abort()</div>
<div class="description-col" style="height:18px;">Cancel the current request.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:68px;">getAllResponseHeaders()</div>
<div class="description-col" style="height:68px;">The getAllResponseHeaders() method returns all the response headers as a single string with each header on a separate line. The method returns null if readyState value is not 3 or 4.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:86px;">getResponseHeader(DOMString header)</div>
<div class="description-col" style="height:86px;">Returns the value of the specified HTTP header. Call getResponseHeader() only when the readyState value is 3 or 4 (in other words, after the response headers are available); otherwise, the method returns an empty string.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:210px;">open(DOMString method, DOMString url, boolean async, DOMString username, DOMString password)</div>
<div class="description-col" style="height:210px;">
          					Specify the method, url, and other optional attributes of a request.<br />
          					The method parameter can have a value of &#8220;GET&#8221;, &#8220;POST&#8221;, or &#8220;HEAD&#8221;. Other HTTP methods, such as &#8220;PUT&#8221; and &#8220;DELETE&#8221; (primarily used in REST applications), may be possible.<br />
          					The <strong>async</strong> parameter specifies whether the request should be handled asynchronously or not. &#8220;true&#8221; means that script processing carries on after the send() method, without waiting for a response, and &#8220;false&#8221; means that the script waits for a response before continuing script processing.
          				</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:510px;">send()</div>
<div class="description-col" style="height:510px;">
          					After preparing a request by calling the open() method, you send the request to the server. You may call send() only when the readyState value is 1, otherwise the XMLHttpRequest object raises an exception. The request gets sent to the server using the parameters supplied to the open() method. The send() method returns immediately when the async parameter is true, letting other client script processing continue. The XMLHttpRequest object sets the readyState value to 2 (Sent) after the send() method has been called. When the server responds, before receiving the message body, if any, the XMLHttpRequest object sets readState to 3 (Receiving). When the request has completed loading it sets readyState to 4 (Loaded). For a request of type HEAD, it sets the readyState value to 4 immediately after setting it to 3.<br />
          					The send() method takes an optional parameter that may contain data of varying types. Typically, you use this to send data to the server using the POST method. You can explicitly invoke the send() method with null, which is the same as invoking it with no argument. For most other data types, set the Content-Type header using the setReuqestHeader() method before invoking the send() method. If the data parameter in the send(data) method is of type DOMString, encode the data as UTF-8. If data is of type Document, serialize the data using the encoding specified by data.xmlEncoding, if supported or UTF-8 otherwise.
          				</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:74px;">setRequestHeader(DOMString header, DOMString value)</div>
<div class="description-col" style="height:74px;">The setRequestHeader() method sets request headers. You may call this method after calling the open() method when the readyState value is 1; otherwise you will get an exception.</div>
</p></div>
</p></div>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Create an XMLHttpRequest Object</div>
</p></div>
</p></div>
</p></div>
</p></div>
<pre>
          			<code class="text-indent-0"><span class="keyword">function</span> <span class="variable">createHttpRequest()</span> <span class="keyword">{</span></code>
          				<code class="text-indent-1"><span class="keyword">var</span> <span class="variable">httpRequest</span> = <span class="value">null</span>;</code>
          					<code class="text-indent-2"><span class="keyword">if (</span><span class="element">window</span>.<span class="keyword">ActiveXObject</span><span class="keyword">)</span> <span class="comment">// For IE6 and below</span></code>
          						<code class="text-indent-3"><span class="variable">httpRequest</span> = <span class="keyword">new</span> <span class="keyword">ActiveXObject(</span><span class="string">"Microsoft.XMLHTTP"</span><span class="keyword">)</span>;</code>
          					<code class="text-indent-2"><span class="keyword">else</span> <span class="comment">// For IE7 and Modern Browsers</span></code>
          						<code class="text-indent-3"><span class="variable">httpRequest</span> = <span class="keyword">new</span> <span class="keyword">XMLHttpRequest()</span>;</code>
          				<code class="text-indent-1"><span class="keyword">return</span> <span class="variable">httpRequest</span>;</code>
          			<code class="text-indent-0"><span class="keyword">}</span></code>
          		</pre>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Submit a Request</div>
</p></div>
</p></div>
</p></div>
</p></div>
<pre>
          			<code class="text-indent-0"><span class="keyword">XMLHttpRequest.open(</span><span class="string">"GET"</span>, <span class="variable">url</span>, <span class="value">true</span><span class="keyword">)</span>;</code>
          		</pre>
<p>If you pass <span class="highlighted">false</span> as the third argument to open(), the send() method is <span class="highlighted">synchronous</span>: it blocks and does not return until the server&#8217;s response has arrived. Asynchronous responses are generally preferred.</p>
<h4 class="subtitle align-subtitle">HTTP GET</h4>
<pre>
          			<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">xmlHttpRequest</span> = <span class="element">window</span>.<span class="keyword">ActiveXObject</span> <span class="keyword">?</span> <span class="keyword">new</span> <span class="keyword">ActiveXObject(</span><span class="string">"Microsoft.XMLHTTP"</span><span class="keyword">)</span> <span class="keyword">:</span> <span class="keyword">new</span> <span class="keyword">XMLHttpRequest()</span>;</code>
          			<code class="text-indent-0"><span class="variable">xmlHttpRequest</span>.<span class="keyword">onreadystatechange = function() {</span></code>

          			<code class="text-indent-0"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="variable">xmlHttpRequest</span>.<span class="keyword">open(</span><span class="string">"GET"</span>, <span class="string">"query.php?username=vivian"</span>, <span class="value">false</span><span class="keyword">)</span>;</code>
          			<code class="text-indent-0"><span class="variable">xmlHttpRequest</span>.<span class="keyword">send(</span><span class="value">null</span><span class="keyword">)</span>;</code>
          		</pre>
<h4 class="subtitle align-subtitle">HTTP POST</h4>
<pre>
          			<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">xmlHttpRequest</span> = <span class="element">window</span>.<span class="keyword">ActiveXObject</span> <span class="keyword">?</span> <span class="keyword">new</span> <span class="keyword">ActiveXObject(</span><span class="string">"Microsoft.XMLHTTP"</span><span class="keyword">)</span> <span class="keyword">:</span> <span class="keyword">new</span> <span class="keyword">XMLHttpRequest()</span>;</code>
          			<code class="text-indent-0"><span class="variable">xmlHttpRequest</span>.<span class="keyword">onreadystatechange = function() {</span></code>

          			<code class="text-indent-0"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="variable">xmlHttpRequest</span>.<span class="keyword">open(</span><span class="string">"POST"</span>, <span class="string">"query.php"</span>, <span class="value">false</span><span class="keyword">)</span>;</code>
          			<code class="text-indent-0"><span class="variable">xmlHttpRequest</span>.<span class="keyword">setRequestHeader(</span><span class="string">"Content-Type"</span>, <span class="value">"application/x-www-form-urlencoded"</span><span class="keyword">)</span>;</code>
          			<code class="text-indent-0"><span class="variable">xmlHttpRequest</span>.<span class="keyword">send(</span><span class="string">"username=vivian"</span><span class="keyword">)</span>;</code>
          		</pre>
<p>With POST requests, data is passed to the server in the body of the request, rather than encoding it into the URL itself. Since request parameters are encoded into the URL of a GET request, the GET method is suitable only when the request has no side effects on the server &#8212; that is, when repeated GET requests for the same URL with the same parameters can be expected to return the same result. When there are side effects to a request (such as when the server stores some of the parameters in a database), a POST request should be used instead.</p>
<h4 class="subtitle align-subtitle">Escape the Data</h4>
<pre>
          			<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">HTTPUTILITY</span> <span class="keyword">= {</span></code>
          				<code class="text-indent-1"><span class="variable">encodeFormData</span><span class="keyword">: function(</span><span class="variable">data</span><span class="keyword">) {</span></code>
          					<code class="text-indent-2"><span class="keyword">if (</span><span class="keyword">!</span><span class="variable">data</span><span class="keyword">)</span> <span class="keyword">return</span>;</code>
          					<code class="text-indent-2"><span class="keyword">var</span> <span class="variable">pairs</span> <span class="keyword">=</span> <span class="keyword">[]</span>;</code>
          					<code class="text-indent-2"><span class="keyword">var</span> <span class="variable">regexp</span> <span class="keyword">=</span> <span class="value">/%20/g</span>; <span class="comment">// A regular expression to match an encoded space.</span></code>
          					<code class="text-indent-2"><span class="keyword">for (</span><span class="keyword">var</span> <span class="string">name</span> <span class="keyword">in</span> <span class="variable">data</span><span class="keyword">) {</span></code>
          						<code class="text-indent-3"><span class="comment">// Create a name/value pair, but encode name and value first. The global function encodeURIComponent does almost what we want,</span></code>
          						<code class="text-indent-3"><span class="comment">// but it encodes spaces as %20 instead of as "+". We have to fix that with String.replace().</span></code>
          						<code class="text-indent-3"><span class="keyword">var</span> <span class="variable">value</span> <span class="keyword">=</span> <span class="variable">data</span><span class="keyword">[</span><span class="string">name</span><span class="keyword">]</span>.<span class="keyword">toString()</span>;</code>
          						<code class="text-indent-3"><span class="keyword">var</span> <span class="variable">pair</span> <span class="keyword">=</span> <span class="keyword">encodeURIComponent(</span><span class="string">name</span><span class="keyword">)</span>.<span class="keyword">replace(</span><span class="variable">regexp</span>, <span class="string">"+"</span><span class="keyword">)</span> <span class="keyword">+</span> <span class="string">"="</span> <span class="keyword">+</span> <span class="keyword">encodeURIComponent(</span><span class="value">value</span><span class="keyword">)</span>.<span class="keyword">replace(</span><span class="variable">regexp</span>, <span class="string">"+"</span><span class="keyword">)</span>;</code>
          						<code class="text-indent-3"><span class="variable">pairs</span>.<span class="keyword">push(</span><span class="variable">pair</span><span class="keyword">)</span>;</code>
          					<code class="text-indent-2"><span class="keyword">}</span></code>
          					<code class="text-indent-2"><span class="keyword">return</span> <span class="variable">pairs</span>.<span class="keyword">join(</span><span class="string">"&#038;"</span><span class="keyword">)</span>; <span class="comment">// Concatenate all the name/value pairs, separating them with &#038;.</span></code>
          				<code class="text-indent-1"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="keyword">};</span></code>
          		</pre>
<p>The POST method uses the AJAX.encodeFormData() function to convert the properties of an object to a string form that can be used as the body of a POST request. This string is then passed to the XMLHttpRequest.send() method and becomes the body of the request. (The string returned by AJAX.encodeFormData() can also be appended to a GET URL; just use a quest-mark character to separate the URL from the data.)</p>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Retrieve the Response</div>
</p></div>
</p></div>
</p></div>
</p></div>
<pre>
          			<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">xmlHttpRequest</span> = <span class="element">window</span>.<span class="keyword">ActiveXObject</span> <span class="keyword">?</span> <span class="keyword">new</span> <span class="keyword">ActiveXObject(</span><span class="string">"Microsoft.XMLHTTP"</span><span class="keyword">)</span> <span class="keyword">:</span> <span class="keyword">new</span> <span class="keyword">XMLHttpRequest()</span>;</code>
          			<code class="text-indent-0"><span class="variable">xmlHttpRequest</span>.<span class="keyword">onreadystatechange = function() {</span></code>
          				<code class="text-indent-1"><span class="keyword">if (</span><span class="variable">xmlHttpRequest</span>.<span class="attribute">readyState</span> <span class="keyword">!=</span> <span class="value">4</span><span class="keyword">) {</span></code>
          					<code class="text-indent-2"><span class="comment">// Start to progress</span></code>
          				<code class="text-indent-1"><span class="keyword">} else {</span> <span class="comment">// The server's response is complete.</span></code>
          					<code class="text-indent-2"><span class="keyword">if (</span><span class="variable">xmlHttpRequest</span>.<span class="attribute">status</span> <span class="keyword">==</span> <span class="value">0</span> <span class="keyword">||</span> <span class="variable">xmlHttpRequest</span>.<span class="attribute">status</span> <span class="keyword">==</span> <span class="value">200</span><span class="keyword">)</span></code>
          						<code class="text-indent-3"><span class="variable">callback</span><span class="keyword">(</span><span class="variable">xmlHttpRequest</span>.<span class="attribute">responseText</span><span class="keyword">)</span></code>
          					<code class="text-indent-2"><span class="keyword">else</span></code>
          						<code class="text-indent-3"><span class="comment">// Error</span></code>
          				<code class="text-indent-1"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="variable">xmlHttpRequest</span>.<span class="keyword">open(</span><span class="string">"POST"</span>, <span class="string">"query.php"</span>, <span class="value">false</span><span class="keyword">)</span>;</code>
          			<code class="text-indent-0"><span class="variable">xmlHttpRequest</span>.<span class="keyword">setRequestHeader(</span><span class="string">"Content-Type"</span>, <span class="value">"application/x-www-form-urlencoded"</span><span class="keyword">)</span>;</code>
          			<code class="text-indent-0"><span class="variable">xmlHttpRequest</span>.<span class="keyword">send(</span><span class="string">"username=vivian"</span><span class="keyword">)</span>;</code>
          		</pre>
<h4 class="subtitle align-subtitle">Notes on readyState 3</h4>
<p>Data is being received from the server.<span class="highlighted">readyState 3</span> differs somewhat in Firefox and Internet Explorer. Firefox invokes the <span class="highlighted">onreadystatechange</span> handler multiple times in <span class="highlighted">readyState 3</span>, to provide download progress feedback. A script might use these multiple invocations to display a progress indicator to the user. Internet Explorer, on the other hand, interprets the event handler name strictly, and invokes it only when the readyState value actualy changes. This means that it is invoked only once for readyState 3, no matter how large downloaded document is.</p>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Time Out a Request</div>
</p></div>
</p></div>
</p></div>
</p></div>
<pre>
          			<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">timer</span> <span class="keyword">=</span> <span class="value">null</span>;</code>
          			<code class="text-indent-0"><span class="keyword">var<span class="keyword"> <span class="variable">timeout</span> <span class="keyword">=</span> <span class="value">1000</span>;</code>
          			<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">xmlHttpRequest</span> = <span class="element">window</span>.<span class="keyword">ActiveXObject</span> <span class="keyword">?</span> <span class="keyword">new</span> <span class="keyword">ActiveXObject(</span><span class="string">"Microsoft.XMLHTTP"</span><span class="keyword">)</span> <span class="keyword">:</span> <span class="keyword">new</span> <span class="keyword">XMLHttpRequest()</span>;</code>
          			<code class="text-indent-0"><span class="keyword">if (</span><span class="variable">timeout</span><span class="keyword">) {</span></code>
          				<code class="text-indent-1"><span class="keyword">if (</span><span class="variable">timer</span><span class="keyword">)</span> <span class="keyword">clearTimeout(</span><span class="variable">timer</span><span class="keyword">)</span>;</code>
          				<code class="text-indent-1"><span class="variable">timer</span> <span class="keyword">=</span> <span class="keyword">setTimeout(</span><span class="keyword">function(){</span></code>
          					<code class="text-indent-2"><span class="variable">xmlHttpRequest</span>.<span class="keyword">abort()</span>;</code>
          				<code class="text-indent-1"><span class="keyword">}</span>, <span class="variable">timeout</span><span class="keyword">)</span>;</code>
          			<code class="text-indent-0"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="variable">xmlHttpRequest</span>.<span class="keyword">onreadystatechange = function() {</span></code>
          				<code class="text-indent-1"><span class="keyword">if (</span><span class="variable">xmlHttpRequest</span>.<span class="attribute">readyState</span> <span class="keyword">!=</span> <span class="value">4</span><span class="keyword">) {</span></code>
          					<code class="text-indent-2"><span class="comment">// Start to progress</span></code>
          				<code class="text-indent-1"><span class="keyword">} else {</span> <span class="comment">// The server's response is complete.</span></code>
          					<code class="text-indent-2"><span class="keyword">if (</span><span class="variable">timer</span><span class="keyword">)</span> <span class="keyword">clearTimeout(</span><span class="variable">timer</span><span class="keyword">)</span>;</code>
          					<code class="text-indent-2"><span class="keyword">if (</span><span class="variable">xmlHttpRequest</span>.<span class="attribute">status</span> <span class="keyword">==</span> <span class="value">0</span> <span class="keyword">||</span> <span class="variable">xmlHttpRequest</span>.<span class="attribute">status</span> <span class="keyword">==</span> <span class="value">200</span><span class="keyword">)</span></code>
          						<code class="text-indent-3"><span class="variable">callback</span><span class="keyword">(</span><span class="variable">xmlHttpRequest</span>.<span class="attribute">responseText</span><span class="keyword">)</span></code>
          					<code class="text-indent-2"><span class="keyword">else</span></code>
          						<code class="text-indent-3"><span class="comment">// Error</span></code>
          				<code class="text-indent-1"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="variable">xmlHttpRequest</span>.<span class="keyword">open(</span><span class="string">"POST"</span>, <span class="string">"query.php"</span>, <span class="value">false</span><span class="keyword">)</span>;</code>
          			<code class="text-indent-0"><span class="variable">xmlHttpRequest</span>.<span class="keyword">setRequestHeader(</span><span class="string">"Content-Type"</span>, <span class="value">"application/x-www-form-urlencoded"</span><span class="keyword">)</span>;</code>
          			<code class="text-indent-0"><span class="variable">xmlHttpRequest</span>.<span class="keyword">send(</span><span class="string">"username=vivian"</span><span class="keyword">)</span>;</code>
          		</pre>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Prevent Caching for Internet Explorer</div>
</p></div>
</p></div>
</p></div>
</p></div>
<p>By appending the julian day number to the end of the URL(?timestamp=123412341234), we effectively make each call unique and that means Internet Explorer will not cache the Ajax call as it is wont to do.</p>
<div class="button-row clearfix">
<p class="button-style preview-icon"><a href="http://www.lingihuang.com/viviancrap/wp-content/uploads/xmlhttprequest_object/index.html" target="_blank">View<span>Demo</span></a></p>
</p></div>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Related Posts</div>
</p></div>
</p></div>
</p></div>
</p></div>
<ul class="entry-list">
<li><a href="http://www.w3.org/TR/XMLHttpRequest/" target="_blank">The XMLHttpRequest Object</a></li>
<li><a href="http://www.w3.org/TR/XMLHttpRequest2/" target="_blank">XMLHttpRequest Level 2</a></li>
<li><a href="http://www.w3.org/TR/access-control/" target="_blank">Access Control</a></li>
<li><a href="http://www.hunlock.com/blogs/The_Ultimate_Ajax_Object" target="_blank">The Ultimate AJAX Object</a></li>
</ul></div>
]]></content:encoded>
			<wfw:commentRss>http://www.lingihuang.com/viviancrap/archives/the-xmlhttprequest-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Window onResize Handler</title>
		<link>http://www.lingihuang.com/viviancrap/archives/window-onresize-handler/</link>
		<comments>http://www.lingihuang.com/viviancrap/archives/window-onresize-handler/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 14:58:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.lingihuang.com/viviancrap/?p=256</guid>
		<description><![CDATA[
There is a bug in IE (version 6 and 7) in which the clarity between window.onresize and body.onresize is muddied. This can cause quite a problem for developers and even render the client&#8217;s browser unresponsive (throttling the CPU to near 100%) if you&#8217;re not very careful. IE fires the onresize multiple times while Firefox only [...]]]></description>
			<content:encoded><![CDATA[<div class="entry-content">
<p>There is a bug in IE (version 6 and 7) in which the clarity between <span class="highlighted">window.onresize</span> and <span class="highlighted">body.onresize</span> is muddied. This can cause quite a problem for developers and even render the client&#8217;s browser unresponsive (throttling the CPU to near 100%) if you&#8217;re not very careful. IE fires the onresize multiple times while Firefox only fires it once after the browser has completed resizing.</p>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Prevent IE from Firing onResize Multiple Times</div>
</p></div>
</p></div>
</p></div>
</p></div>
<ul class="entry-list">
<li>Solution One: setTimeout()</li>
<pre>
          				<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">elements</span> = <span class="keyword">{</span><span class="string">"wrapper":</span> <span class="value">null</span>, <span class="string">"topHeader":</span> <span class="value">null</span>, <span class="string">"bottomFooter":</sapn> <span class="value">null</span>, <span class="string">"leftSide":</span> <span class="value">null</span>, <span class="string">"rightSide":</sapn> <span class="value">null</span>, <span class="string">"content":</sapn> <span class="value">null</sapn><span class="keyword">}</sapn>;</code>
          				<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">properties</span> = <span class="keyword">[</span><span class="value">"clientWidth"</span>, <span class="value">"clientHeight"</span>, <span class="value">"offsetWidth"</span>, <span class="value">"offsetHeight"</span><span class="keyword">]</span>;</code>
          				<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">resizeTimeout</span> = <span class="value">null</span>;</code>
          				<code class="text-indent-0"><span class="keyword">var</sapn> <span class="variable">onloadHandler</span> <span class="keyword">= function() {</span></code>
          					<code class="text-indent-1"><span class="keyword">for (</span><span class="keyword">var</span> <span class="string">name</span> <span class="keyword">in</span> <span class="variable">elements</span><span class="keyword">)</span></code>
          						<code class="text-indent-2"><span class="variable">elements</span><span class="keyword">[</span><span class="string">name</span><span class="keyword">]</span> <span class="keyword">=</span> <span class="element">document</span>.<span class="keyword">getElementById(</span><span class="string">name</span><span class="keyword">)</span>;</code>
          					<code class="text-indent-1"><span class="keyword">onresizeHandler();</span></code>
          				<code class="text-indent-0"><span class="keyword">};</span></code>
          				<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">onresizeHandler</span> <span class="keyword">= function() {</span></code>
          					<code class="text-indent-1"><span class="keyword">if (</span><span class="variable">resizeTimeout</span><span class="keyword">)</span> <span class="keyword">clearTimeout(</span><span class="variable">resizeTimeout</span><span class="keyword">)</span>;</code>
          					<code class="text-indent-1"><span class="variable">resizeTimeout</span> <span class="keyword">=</span> <span class="keyword">setTimeout(function(){</span></code>
          						<code class="text-indent-2"><span class="keyword">var</span> <span class="variable">height</span> <span class="keyword">=</span> <span class="variable">GEOMETRY</span>.<span class="keyword">getViewportHeight()</span> <span class="keyword">-</span> <span class="element">elements.topHeader</span>.<span class="attribute">offsetHeight</span> <span class="keyword">-</span> <span class="element">elements.bottomFooter</span>.<span class="attribute">offsetHeight</span>;</code>
          						<code class="text-indent-2"><span class="variable">height</span> <span class="keyword">-=</span> <span class="keyword">parseInt(</span><span class="variable">CSS</span>.<span class="keyword">get(</span><span class="element">elements.topHeader</span>, <span class="string">"margin-top"</span><span class="keyword">)</span>, <span class="value">10</span><span class="keyword">)</span> <span class="keyword">-</span> <span class="keyword">parseInt(</span><span class="variable">CSS</span>.<span class="keyword">get(</span><span class="element">elements.topHeader</span>, <span class="string">"margin-bottom"</span><span class="keyword">)</span>, <span class="value">10</span><span class="keyword">)</span> <span class="keyword">-</span> <span class="keyword">parseInt(</span><span class="variable">CSS</span>.<span class="keyword">get(</span><span class="element">elements.bottomFooter</span>, <span class="string">"margin-top"</span><span class="keyword">)</span>, <span class="value">10</span><span class="keyword">)</span> <span class="keyword">-</span> <span class="keyword">parseInt(</span><span class="variable">CSS</span>.<span class="keyword">get(</span><span class="element">elements.bottomFooter</span>, <span class="string">"margin-bottom"</span><span class="keyword">)</span>, <span class="value">10</span><span class="keyword">)</span>;</code>
          						<code class="text-indent-2"><span class="variable">CSS</span>.<span class="keyword">set(</span><span class="element">elements.leftSide</span>, <span class="keyword">{</span><span class="string">"height":</span> <span class="value">height</span><span class="keyword">}</span><span class="keyword">)</span>;</code>
          						<code class="text-indent-2"><span class="variable">CSS</span>.<span class="keyword">set(</span><span class="element">elements.rightSide</span>, <span class="keyword">{</span><span class="string">"height":</span> <span class="value">height</span><span class="keyword">}</span><span class="keyword">)</span>;</code>
          						<code class="text-indent-2"><span class="variable">CSS</span>.<span class="keyword">set(</span><span class="element">elements.content</span>, <span class="keyword">{</span><span class="string">"height":</span> <span class="value">height</span><span class="keyword">}</span><span class="keyword">)</span>;</code>
          					<code class="text-indent-1"><span class="keyword">}</span>, <span class="value">400</span><span class="keyword">)</span>;</code>
          				<code class="text-indent-0"><span class="keyword">};</span></code>
          				<code class="text-indent-0"><span class="variable">GENERIC</span>.<span class="keyword">addEvent(</span><span class="element">window</span>, <span class="string">"load"</span>, <span class="variable">onloadHandler</span><span class="keyword">)</span>;</code>
          				<code class="text-indent-0"><span class="variable">GENERIC</span>.<span class="keyword">addEvent(</span><span class="element">window</span>, <span class="string">"resize"</span>, <span class="variable">onresizeHandler</span><span class="keyword">)</span>;</code>
          			</pre>
<pre>
          				<code class="text-indent-0"><span class="element">&lt;div</span> <span class="id">id</span>=<span class="string">"wrapper"</span> <span class="class">class</span>=<span class="string">"wrapper"</span><span class="element">&gt;</span></code>
          					<code class="text-indent-1"><span class="element">&lt;div</span> <span class="id">id</span>=<span class="string">"topHeader"</span> <span class="class">class</span>=<span class="string">"top-header"</span><span class="element">&gt;</span>Header<span class="element">&lt;/div&gt;</span></code>
          					<code class="text-indent-1"><span class="element">&lt;div</span> <span class="id">id</span>=<span class="string">"leftSide"</span> <span class="class">class</span>=<span class="string">"side-bar align-left"</span><span class="element">&gt;</span>Side Bar<span class="element">&lt;/div&gt;</span></code>
          					<code class="text-indent-1"><span class="element">&lt;div</span> <span class="id">id</span>=<span class="string">"rightSide"</span> <span class="class">class</span>=<span class="string">"side-bar align-right"</span><span class="element">&gt;</span>Side Bar<span class="element">&lt;/div&gt;</span></code>
          					<code class="text-indent-1"><span class="element">&lt;div</span> <span class="id">id</span>=<span class="string">"content"</span> <span class="class">class</span>=<span class="string">"content"</span><span class="element">&gt;</span>Content<span class="element">&lt;/div&gt;</span></code>
          					<code class="text-indent-1"><span class="element">&lt;div</span> <span class="id">id</span>=<span class="string">"bottomFooter"</span> <span class="class">class</span>=<span class="string">"bottom-footer"</span><span class="element">&gt;</span>Footer<span class="element">&lt;/div&gt;</span></code>
          				<code class="text-indent-0"><span class="element">&lt;/div&gt;</span></code>
          			</pre>
<div class="button-row clearfix">
<p class="button-style preview-icon"><a href="http://www.lingihuang.com/viviancrap/wp-content/uploads/window_onresize_handler/setTimeout.html" target="_blank">View<span>Demo</span></a></p>
</p></div>
<li>Solution Two: setInterval()</li>
<pre>
          				<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">elements</span> = <span class="keyword">{</span><span class="string">"wrapper":</span> <span class="value">null</span>, <span class="string">"topHeader":</span> <span class="value">null</span>, <span class="string">"bottomFooter":</sapn> <span class="value">null</span>, <span class="string">"leftSide":</span> <span class="value">null</span>, <span class="string">"rightSide":</sapn> <span class="value">null</span>, <span class="string">"content":</sapn> <span class="value">null</sapn><span class="keyword">}</sapn>;</code>
          				<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">properties</span> = <span class="keyword">[</span><span class="value">"clientWidth"</span>, <span class="value">"clientHeight"</span>, <span class="value">"offsetWidth"</span>, <span class="value">"offsetHeight"</span><span class="keyword">]</span>;</code>
          				<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">initHeight</span> = <span class="value">0</span>;</code>
          				<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">resizeInterval</span> = <span class="value">null</span>;</code>
          				<code class="text-indent-0"><span class="keyword">var</sapn> <span class="variable">onloadHandler</span> <span class="keyword">= function() {</span></code>
          					<code class="text-indent-1"><span class="keyword">for (</span><span class="keyword">var</span> <span class="string">name</span> <span class="keyword">in</span> <span class="variable">elements</span><span class="keyword">)</span></code>
          						<code class="text-indent-2"><span class="variable">elements</span><span class="keyword">[</span><span class="string">name</span><span class="keyword">]</span> <span class="keyword">=</span> <span class="element">document</span>.<span class="keyword">getElementById(</span><span class="string">name</span><span class="keyword">)</span>;</code>
          					<code class="text-indent-1"><span class="keyword">if (</span><span class="variable">resizeInterval</span><span class="keyword">)</span> <span class="keyword">clearInterval(</span><span class="variable">resizeInterval</span><span class="keyword">)</span>;</code>
          					<code class="text-indent-1"><span class="variable">resizeInterval</span> <span class="keyword">=</span> <span class="keyword">setInterval(function(){</span></code>
          						<code class="text-indent-2"><span class="keyword">onresizeHandler()</span>;</code>
          					<code class="text-indent-1"><span class="keyword">}</span>, <span class="value">100</span><span class="keyword">)</span>;</code>
          				<code class="text-indent-0"><span class="keyword">};</span></code>
          				<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">onresizeHandler</span> <span class="keyword">= function() {</span></code>
          					<code class="text-indent-1"><span class="keyword">var</span> <span class="variable">height</span> <span class="keyword">=</span> <span class="variable">GEOMETRY</span>.<span class="keyword">getViewportHeight()</span> <span class="keyword">-</span> <span class="element">elements.topHeader</span>.<span class="attribute">offsetHeight</span> <span class="keyword">-</span> <span class="element">elements.bottomFooter</span>.<span class="attribute">offsetHeight</span>;</code>
          					<code class="text-indent-1"><span class="variable">height</span> <span class="keyword">-=</span> <span class="keyword">parseInt(</span><span class="variable">CSS</span>.<span class="keyword">get(</span><span class="element">elements.topHeader</span>, <span class="string">"margin-top"</span><span class="keyword">)</span>, <span class="value">10</span><span class="keyword">)</span> <span class="keyword">-</span> <span class="keyword">parseInt(</span><span class="variable">CSS</span>.<span class="keyword">get(</span><span class="element">elements.topHeader</span>, <span class="string">"margin-bottom"</span><span class="keyword">)</span>, <span class="value">10</span><span class="keyword">)</span> <span class="keyword">-</span> <span class="keyword">parseInt(</span><span class="variable">CSS</span>.<span class="keyword">get(</span><span class="element">elements.bottomFooter</span>, <span class="string">"margin-top"</span><span class="keyword">)</span>, <span class="value">10</span><span class="keyword">)</span> <span class="keyword">-</span> <span class="keyword">parseInt(</span><span class="variable">CSS</span>.<span class="keyword">get(</span><span class="element">elements.bottomFooter</span>, <span class="string">"margin-bottom"</span><span class="keyword">)</span>, <span class="value">10</span><span class="keyword">)</span>;</code>
          					<code class="text-indent-1"><span class="keyword">if (</span><span class="variable">initHeight</span> <span class="keyword">!=</span> <span class="variable">height</span><span class="keyword">)</span></code>
          						<code class="text-indent-2"><span class="variable">CSS</span>.<span class="keyword">set(</span><span class="element">elements.leftSide</span>, <span class="keyword">{</span><span class="string">"height":</span> <span class="value">height</span><span class="keyword">}</span><span class="keyword">)</span>;</code>
          						<code class="text-indent-2"><span class="variable">CSS</span>.<span class="keyword">set(</span><span class="element">elements.rightSide</span>, <span class="keyword">{</span><span class="string">"height":</span> <span class="value">height</span><span class="keyword">}</span><span class="keyword">)</span>;</code>
          						<code class="text-indent-2"><span class="variable">CSS</span>.<span class="keyword">set(</span><span class="element">elements.content</span>, <span class="keyword">{</span><span class="string">"height":</span> <span class="value">height</span><span class="keyword">}</span><span class="keyword">)</span>;</code>
          						<code class="text-indent-2"><span class="variable">initHeight</span> <span class="keyword">=</span> <span class="variable">height</span>;</code>
          					<code class="text-indent-1"><span class="keyword">}</span></code>
          				<code class="text-indent-0"><span class="keyword">};</span></code>
          				<code class="text-indent-0"><span class="variable">GENERIC</span>.<span class="keyword">addEvent(</span><span class="element">window</span>, <span class="string">"load"</span>, <span class="variable">onloadHandler</span><span class="keyword">)</span>;</code>
          			</pre>
<pre>
          				<code class="text-indent-0"><span class="element">&lt;div</span> <span class="id">id</span>=<span class="string">"wrapper"</span> <span class="class">class</span>=<span class="string">"wrapper"</span><span class="element">&gt;</span></code>
          					<code class="text-indent-1"><span class="element">&lt;div</span> <span class="id">id</span>=<span class="string">"topHeader"</span> <span class="class">class</span>=<span class="string">"top-header"</span><span class="element">&gt;</span>Header<span class="element">&lt;/div&gt;</span></code>
          					<code class="text-indent-1"><span class="element">&lt;div</span> <span class="id">id</span>=<span class="string">"leftSide"</span> <span class="class">class</span>=<span class="string">"side-bar align-left"</span><span class="element">&gt;</span>Side Bar<span class="element">&lt;/div&gt;</span></code>
          					<code class="text-indent-1"><span class="element">&lt;div</span> <span class="id">id</span>=<span class="string">"rightSide"</span> <span class="class">class</span>=<span class="string">"side-bar align-right"</span><span class="element">&gt;</span>Side Bar<span class="element">&lt;/div&gt;</span></code>
          					<code class="text-indent-1"><span class="element">&lt;div</span> <span class="id">id</span>=<span class="string">"content"</span> <span class="class">class</span>=<span class="string">"content"</span><span class="element">&gt;</span>Content<span class="element">&lt;/div&gt;</span></code>
          					<code class="text-indent-1"><span class="element">&lt;div</span> <span class="id">id</span>=<span class="string">"bottomFooter"</span> <span class="class">class</span>=<span class="string">"bottom-footer"</span><span class="element">&gt;</span>Footer<span class="element">&lt;/div&gt;</span></code>
          				<code class="text-indent-0"><span class="element">&lt;/div&gt;</span></code>
          			</pre>
<div class="button-row clearfix">
<p class="button-style preview-icon"><a href="http://www.lingihuang.com/viviancrap/wp-content/uploads/window_onresize_handler/setInterval.html" target="_blank">View<span>Demo</span></a></p>
</p></div>
</ul>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Related Posts</div>
</p></div>
</p></div>
</p></div>
</p></div>
<ul class="entry-list">
<li><a href="http://social.msdn.microsoft.com/Forums/en-US/MSWinWebChart/thread/f01d6ab8-01a7-417b-b2ac-e94e677b2182" target="_blank">Is it possible to have .NET chart auto-resize to fill area on a page?</a></li>
<li><a href="http://blog.stchur.com/2006/09/06/the-ie-resize-bug-revisited/" target="_blank">Beat the IE Resize Bug</a></li>
<li><a href="http://www.jaceju.net/blog/?p=268" target="_blank">window.onresize 連續觸發的問題</a></li>
</ul></div>
]]></content:encoded>
			<wfw:commentRss>http://www.lingihuang.com/viviancrap/archives/window-onresize-handler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Work with Style Properties</title>
		<link>http://www.lingihuang.com/viviancrap/archives/work-with-style-properties/</link>
		<comments>http://www.lingihuang.com/viviancrap/archives/work-with-style-properties/#comments</comments>
		<pubDate>Sun, 05 Jul 2009 15:48:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.lingihuang.com/viviancrap/?p=238</guid>
		<description><![CDATA[




Scripting Computed Styles




The W3C standard API for determining the computed style of an element is the getComputedStyle() method of the Window object. The first argument to this method is the element whose computed style is desired. The second argument is any CSS pseudoelement, such as &#8220;:before&#8221; or &#8220;:after&#8221; whose style is desired. In the Mozilla [...]]]></description>
			<content:encoded><![CDATA[<div class="entry-content">
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Scripting Computed Styles</div>
</p></div>
</p></div>
</p></div>
</p></div>
<p>The W3C standard API for determining the computed style of an element is the <span class="highlighted">getComputedStyle()</span> method of the <span class="highlighted">Window object</span>. The first argument to this method is the element whose computed style is desired. The second argument is any <span class="highlighted">CSS pseudoelement</span>, such as <span class="minor">&#8220;:before&#8221;</span> or <span class="minor">&#8220;:after&#8221;</span> whose style is desired. In the Mozilla and Firefox implementation of this method, the second argument is required and may not be omitted. As a result, you&#8217;ll usually see getComputedStyle() invoked with <span class="highlighted">null</span> as its second argument.</p>
<p>IE provides a simpler alternative: every HTML element has a <span class="highlighted">currentStyle</span> property that holds its computed style.</p>
<pre>
          			<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">CSS</span> <span class="keyword">=  {</span></code>
						<code class="text-indent-1"><span class="variable">getStyle</span><span class="keyword">: function(</span><span class="element">element</span><span class="keyword">) {</span></code>
							<code class="text-indent-2"><span class="keyword">if (</span><span class="keyword">!</span><span class="element">element</span><span class="keyword">)</span> <span class="keyword">return</span>;</code>
							<code class="text-indent-2"><span class="keyword">if (</span><span class="keyword">typeof</span> <span class="element">element</span> <span class="keyword">==</span> <span class="string">"string"</span><span class="keyword">)</span> <span class="element">element</span> <span class="keyword">=</span> <span class="element">document</span>.<span class="keyword">getElementById(</span><span class="string">element</span><span class="keyword">)</span>;</code>
							<code class="text-indent-2"><span class="keyword">if (</span><span class="element">element</span>.<span class="attribute">currentStyle</span><span class="keyword">)</span> <span class="comment">// For IE</span></code>
								<code class="text-indent-3"><span class="keyword">return</span> <span class="element">element</span>.<span class="attribute">currentStyle</span>;</code>
							<code class="text-indent-2"><span class="keyword">else if (</span><span class="element">document</span>.<span class="attribute">defaultView</span> <span class="keyword">&#038;&#038;</span> <span class="element">document</span>.<span class="attribute">defaultView</span>.<span class="keyword">getComputedStyle</span><span class="keyword">)</span> <span class="comment">// For Modern Browsers</span></code>
								<code class="text-indent-3"><span class="keyword">return</span> <span class="element">document</span>.<span class="attribute">defaultView</span>.<span class="keyword">getComputedStyle(</span><span class="element">element</span>, <span class="value">null</span><span class="keyword">)</span>;</code>
							<code class="text-indent-2"><span class="keyword">else</span></code>
								<code class="text-indent-3"><span class="keyword">return</span> <span class="element">element</span>.<span class="attribute">style</span>;</code>
						<code class="text-indent-1"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="keyword">};</span></code>
          		</pre>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Retrive Global/External CSS Properties</div>
</p></div>
</p></div>
</p></div>
</p></div>
<p>These two approaches frequently require different ways of referring to the style properties. In the case of the IE <span class="highlighted">currentStyle</span> object, references are made via the same object model syntax as is used for getting and setting style values. Therefore, hyphenated CSS property names must be referenced via the <span class="highlighted">intercapitalization system</span> (e.g., <span class="minor">margin-left</span> becomes <span class="minor">marginLeft</span>). But the property name for the W3C DOM <span class="highlighted">getPropertyValue()</span> method must be in the CSS property format (e.g., <span class="minor">margin-left</span> is <span class="minor">margin-left</span>).</p>
<p>Also be aware that for some CSS properties, different browser versions may return different value types-especially in colors that are specified by CSS syntax other than rgb(r, g, b). For example, if you set the color with a plain-language color name (e.g., orange), the value returned from the browsers may be in a different format. For the most part, if you specify colors in rgb(r, g, b) format, you&#8217;ll get that back.</p>
<p>CSS values consisting of length measurements typically contain units (pixels, points, ems, and so on). If you tend to utilize the value of a style property for any math, such as adding five pixels to the left edge of a positioned element, be sure to extract the numeric portion of sorting values that include units. Use the <span class="highlighted">parseInt()</span> function for integers and the <span class="highlighted">parseFloat()</span> function for numeric values that may have digits to the right of the decimal (e.g., 0.5em).</p>
<p>Once you assign a value to a property of an element&#8217;s <span class="highlighted">style object</span>, the value can be read subsequently through the <span class="highlighted">style property</span>. But for consistency&#8217;s sake, you can continue to read a value the getPropertyValue() method and currentStyle object because it returns the effective value applied to the element at any instant.</p>
<pre>
          			<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">CSS</span> <span class="keyword">=  {</span></code>
						<code class="text-indent-1"><span class="variable">get</span><span class="keyword">: function(</span><span class="element">element</span>, <span class="string">name</span><span class="keyword">) {</span></code>
							<code class="text-indent-2"><span class="keyword">if (</span><span class="keyword">!</span><span class="element">element</span><span class="keyword">)</span> <span class="keyword">return;</span></code>
							<code class="text-indent-2"><span class="keyword">if (</span><span class="keyword">typeof</span> <span class="element">element</span> <span class="keyword">==</span> <span class="string">"string"</span><span class="keyword">)</span> <span class="element">element</span> <span class="keyword">=</span> <span class="element">document</span>.<span class="keyword">getElementById(</span><span class="element">element</span><span class="keyword">);</span></code>
							<code class="text-indent-2"><span class="comment">// Convert "background-color" to "backgroundColor"</span></code>
							<code class="text-indent-2"><span class="keyword">var</span> <span class="variable">camelCase</span> <span class="keyword">=</span> <span class="string">name</span>.<span class="keyword">replace(</span><span class="value">/\-(\w)/g</span>, <span class="keyword">function(</span><span class="variable">all</span>, <span class="variable">letter</span><span class="keyword">) {</span></code>
								<code class="text-indent-3"><span class="keyword">return</span> <span class="variable">letter</span><span class="keyword">.toUpperCase()</span>;</code>
							<code class="text-indent-2"><span class="keyword">});</span></code>
							<code class="text-indent-2"><span class="keyword">if (</span><span class="element">element</span>.<span class="attribute">currentStyle</span><span class="keyword">) {</span> <span class="comment">// For IE</span></code>
								<code class="text-indent-3"><span class="keyword">return</span> <span class="element">element</span>.<span class="attribute">currentStyle</span><span class="keyword">[</span><span class="string">name</span><span class="keyword">]</span> <span class="keyword">||</span> <span class="element">element</span>.<span class="attribute">currentStyle</span><span class="keyword">[</span><span class="variable">camelCase</span><span class="keyword">]</span>;</code>
							<code class="text-indent-2"><span class="keyword">} else if (</span><span class="element">document</span>.<span class="attribute">defaultView</span> <span class="keyword">&#038;&#038;</span> <span class="element">document</span>.<span class="attribute">defaultView</span>.<span class="keyword">getComputedStyle</span><span class="keyword">) {</span> <span class="comment">// // For Modern Browsers</span></code>
								<code class="text-indent-3"><span class="string">name</span> <span class="keyword">=</span> <span class="string">name</span>.<span class="keyword">replace(</span><span class="value">/([A-Z])/g</span>, <span class="string">"-$1"</span><span class="keyword">)</span>.<span class="keyword">toLowerCase()</span>;</code>
								<code class="text-indent-3"><span class="keyword">return</span> <span class="element">document</span>.<span class="attribute">defaultView</span>.<span class="keyword">getComputedStyle(</span><span class="element">element</span>, <span class="value">null</span><span class="keyword">)</span>.<span class="keyword">getPropertyValue(</span><span class="string">name</span><span class="keyword">)</span>;</code>
							<code class="text-indent-2"><span class="keyword">} else {</span></code>
								<code class="text-indent-3"><span class="keyword">return</span> <span class="element">element</span>.<span class="attribute">style</span><span class="keyword">[</span><span class="string">name</span><span class="keyword">]</span> <span class="keyword">||</span> <span class="element">element</span>.<span class="attribute">style</span><span class="keyword">[</span><span class="variable">camelCase</span><span class="keyword">]</span>;</code>
							<code class="text-indent-2"><span class="keyword">}</span></code>
						<code class="text-indent-1"><span class="keyword">},</span></code>
						<code class="text-indent-1"><span class="variable">set</span><span class="keyword">: function(</span><span class="element">element</span>, <span class="variable">properties</span><span class="keyword">) {</span></code>
							<code class="text-indent-2"><span class="keyword">if (</span><span class="keyword">!</span><span class="element">element</span><span class="keyword">)</span> <span class="keyword">return;</span></code>
							<code class="text-indent-2"><span class="keyword">if (</span><span class="keyword">typeof</span> <span class="element">element</span> <span class="keyword">==</span> <span class="string">"string"</span><span class="keyword">)</span> <span class="element">element</span> <span class="keyword">=</span> <span class="element">document</span>.<span class="keyword">getElementById(</span><span class="element">element</span><span class="keyword">);</span></code>
							<code class="text-indent-2"><span class="keyword">for (</span><span class="keyword">var</span> <span class="variable">name</span> <span class="keyword">in</span> <span class="variable">properties</span><span class="keyword">) {</span></code>
								<code class="text-indent-3"><span class="comment">// Convert "background-color" to "backgroundColor"</span></code>
								<code class="text-indent-3"><span class="keyword">var</span> <span class="variable">camelCase</span> <span class="keyword">=</span> <span class="string">name</span>.<span class="keyword">replace(</span><span class="value">/\-(\w)/g</span>, <span class="keyword">function(</span><span class="variable">all</span>, <span class="variable">letter</span><span class="keyword">) {</span></code>
									<code class="text-indent-4"><span class="keyword">return</span> <span class="variable">letter</span><span class="keyword">.toUpperCase()</span>;</code>
								<code class="text-indent-3"><span class="keyword">});</span></code>
								<code class="text-indent-3"><span class="keyword">if (</span><span class="keyword">typeof</span> <span class="variable">properties</span><span class="keyword">[</span><span class="variable">name</span><span class="keyword">]</span> <span class="keyword">==</span> <span class="string">"number"</span><span class="keyword">)</span> <span class="element">element</span>.<span class="attribute">style</span><span class="keyword">[</span><span class="variable">camelCase</span><span class="keyword">]</span> <span class="keyword">=</span> <span class="keyword">(</span><span class="keyword">parseInt(</span><span class="variable">properties</span><span class="keyword">[</span><span class="variable">name</span><span class="keyword">]</span>, <span class="value">10</span><span class="keyword">)</span> <span class="keyword">||</span> <span class="value">0</span><span class="keyword">)</span> <span class="keyword">+</span> <span class="string">"px"</span>;</code>
								<code class="text-indent-3"><span class="keyword">if (</span><span class="keyword">typeof</span> <span class="variable">properties</span><span class="keyword">[</sapn><span class="variable">name</span><span class="keyword">]</span> <span class="keyword">==</span> <span class="string">"string"</span><span class="keyword">)</span> <span class="element">element</span>.<span class="attribute">style</span><span class="keyword">[</span><span class="variable">camelCase</span><span class="keyword">]</span> <span class="keyword">=</span> <span class="variable">properties</span><span class="keyword">[</span><span class="variable">name</sapn><span class="keyword">]<span>;</code>
							<code class="text-indent-2"><span class="keyword">}</span></code>
						<code class="text-indent-1"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="keyword">};</span></code>
          		</pre>
<div class="button-row clearfix">
<p class="button-style preview-icon"><a href="http://www.lingihuang.com/viviancrap/wp-content/uploads/work_with_style_properties/index.html" target="_blank">View<span>Demo</span></a></p>
</p></div>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Related Posts</div>
</p></div>
</p></div>
</p></div>
</p></div>
<ul class="entry-list">
<li><a href="http://www.quirksmode.org/dom/getstyles.html" target="_blank">Javascript Get Styles</a></li>
<li><a href="http://www.oreillynet.com/pub/a/javascript/excerpt/JSDHTMLCkbk_chap5/index5.html" target="_blank">Reading Effective Style Sheet Property Values</a></li>
</ul></div>
]]></content:encoded>
			<wfw:commentRss>http://www.lingihuang.com/viviancrap/archives/work-with-style-properties/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The DOM Event Model Advanced</title>
		<link>http://www.lingihuang.com/viviancrap/archives/the-dom-event-model-advanced/</link>
		<comments>http://www.lingihuang.com/viviancrap/archives/the-dom-event-model-advanced/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 12:45:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.lingihuang.com/viviancrap/?p=185</guid>
		<description><![CDATA[




Mouse Events





onMouseDown: A mouse button has been pressed.
onMouseMove: The mouse has been moved.
onMouseOut: The mouse pointer has left an element.
onMouseOver: The mouse pointer has entered an element.
onMouseUp: A mouse button has been released.
onClick: One mousedown and one mouseup detected on this element.
onDbClick: A mouse button has been double-clicked (clicked twice rapidly).





Mouse Event Properties






Property Name
Description


button
A number [...]]]></description>
			<content:encoded><![CDATA[<div class="entry-content">
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Mouse Events</div>
</p></div>
</p></div>
</p></div>
</p></div>
<ul class="entry-list">
<li>onMouseDown: A mouse button has been pressed.</li>
<li>onMouseMove: The mouse has been moved.</li>
<li>onMouseOut: The mouse pointer has left an element.</li>
<li>onMouseOver: The mouse pointer has entered an element.</li>
<li>onMouseUp: A mouse button has been released.</li>
<li>onClick: One mousedown and one mouseup detected on this element.</li>
<li>onDbClick: A mouse button has been double-clicked (clicked twice rapidly).</li>
</ul>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Mouse Event Properties</div>
</p></div>
</p></div>
</p></div>
</p></div>
<div class="entry-table">
<div class="table-heading-row">
<div class="property-col">Property Name</div>
<div class="description-col">Description</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:72px;">button</div>
<div class="description-col" style="height:72px;">A number that specifies which mouse button changed state during a mousedown, mouseup, or click event. A value of 0 indicates the left button, 1 indicates the middle button, and 2 indicates the right button.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:52px;">altKey, ctrlKey, metaKey, shiftKey</div>
<div class="description-col" style="height:52px;">These four boolean fields indicate whether the Alt, Ctrl, Meta, or Shift keys were held down when a mouse event occurred.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:72px;">clientX/clientY</div>
<div class="description-col" style="height:72px;">Specify the X and Y coordinates of the mouse pointer, relative to the upper-left corner of the browser&#8217;s viewport and do not take document scrolling into account.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:88px;">screenX/screenY</div>
<div class="description-col" style="height:88px;">Specify the X and Y coordinates of the mouse pointer, relative to the upper-left corner of the user&#8217;s monitor. These values are useful if you plan to open a new browser window at or near the location of the mouse event.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:88px;">relatedTarget</div>
<div class="description-col" style="height:88px;">This property refers to a node that is related to the target node of the event. For mouseover events, it is the node that the mouse left when it moved over the target. For mouseout events, it is the node that the mouse entered when leaving the target.</div>
</p></div>
</p></div>
<div class="entry-table">
<div class="table-heading-row">
<div class="w3c-col">W3C Standard</div>
<div class="ie-col">Internet Explorer</div>
<div class="desc-col">Description</div>
</p></div>
<div class="table-row">
<div class="w3c-col" style="height:172px;">button</div>
<div class="ie-col" style="height:172px;">button</div>
<div class="desc-col" style="height:172px;">Still an integer value but the button value is interpreted differently in IE. A value of 0 indicates none, 1 indicates the left button, 2 indicates the right button, 3 indicates the left and right buttons, 4 indicates middle button, 5 indicates the left and right buttons, 6 indicates the middle and right buttons, 7 indicates the left, middle, and right buttons.</div>
</p></div>
<div class="table-row">
<div class="w3c-col" style="height:104px;">relatedTarget</div>
<div class="ie-col" style="height:104px;">fromElement<br />toElement</div>
<div class="desc-col" style="height:104px;">fromElement specifes the document element that the mouse came from for mouseover events. toElement specifies the document element that the mouse has moved to for mouseout events.</div>
</p></div>
</p></div>
<h4 class="subtitle align-subtitle">clientX/clientY</h4>
<pre>
          			<code class="text-indent-0"><span class="keyword">function</span> <span class="variable">onMouseDownHandler</span><span class="keyword">(</span><span class="variable">event</span><span class="keyword">) {</span></code>
          				<code class="text-indent-1"><span class="keyword">var</span> <span class="variable">e</span> = <span class="variable">event</span> <span class="keyword">||</span> <span class="element">window</span>.<span class="keyword">event</span>; <span class="comment">// IE stores events in window object.</span></code>
          				<code class="text-indent-1"><span class="keyword">var</span> <span class="variable">mouseObj</span> = <span class="keyword">{</span><span class="attribute">left:</span> <span class="keyword">e.clientX</span>, <span class="attribute">top:</span> <span class="keyword">e.clientY</span><span class="keyword">}</span>; <span class="comment">// Store the position of the mouse object.</span></code>
          			<code class="text-indent-0"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="element">element</span>.<span class="keyword">onmousedown</span> = <span class="variable">onMouseUpHandler</span>;</code>
          		</pre>
<h4 class="subtitle align-subtitle">currentTarget and relatedTarget/fromElement and toElement</h4>
<pre>
          			<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">GENERIC</span> <span class="keyword">= {</span></code>
          				<code class="text-indent-1"><span class="variable">findCurrentTarget</span><span class="keyword">: function(</span><span class="keyword">event</span>, <span class="element">node</span><span class="keyword">) {</span></code>
          					<code class="text-indent-2"><span class="keyword">var</span> <span class="variable">currentTarget</span>;</code>
          					<code class="text-indent-2"><span class="keyword">if (</span><span class="element">window</span>.<span class="keyword">event</span><span class="keyword">)</span> <span class="comment">// For IE</span></code>
          						<code class="text-indent-3"><span class="variable">currentTarget</span> = <span class="element">node</span>;</code>
          					<code class="text-indent-2"><span class="keyword">else if (</span><span class="keyword">event</span> <span class="keyword">&#038;&#038;</span> <span class="keyword">event</span>.<span class="attribute">currentTarget</span><span class="keyword">)</span> <span class="comment">// For Firefox, Opera</span></code>
          						<code class="text-indent-3"><span class="variable">currentTarget</span> = <span class="keyword">event</span>.<span class="attribute">currentTarget</span>;</code>
          					<code class="text-indent-2"><span class="keyword">return</span> <span class="variable">currentTarget</span>;</code>
          				<code class="text-indent-1"><span class="keyword">},</span></code>
          				<code class="text-indent-1"><span class="variable">findRelatedTarget</span><span class="keyword">: function(event) {</span></code>
          					<code class="text-indent-2"><span class="keyword">var</span> <span class="variable">relatedTarget</span>;</code>
          					<code class="text-indent-2"><span class="keyword">if (</span><span class="element">window</span>.<span class="keyword">event</span><span class="keyword">) {</span> <span class="comment">// For IE</span></code>
          						<code class="text-indent-3"><span class="keyword">if (</span><span class="element">window</span>.<span class="keyword">event</span>.<span class="attribute">fromElement</span><span class="keyword">)</span></code>
          							<code class="text-indent-4"><span class="variable">relatedTarget</span> = <span class="element">window</span>.<span class="keyword">event</span>.<span class="attribute">fromElement</span>;</code>
          						<code class="text-indent-3"><span class="keyword">else if (</span><span class="element">window</span>.<span class="keyword">event</span>.<span class="attribute">toElement</span><span class="keyword">)</span></code>
          							<code class="text-indent-4"><span class="variable">relatedTarget</span> = <span class="element">window</span>.<span class="keyword">event</span>.<span class="attribute">toElement</span>;</code>
          					<code class="text-indent-2"><span class="keyword">} else if (</span><span class="keyword">event</span> <span class="keyword">&#038;&#038;</span> <span class="keyword">event</span>.<span class="attribute">relatedTarget</span><span class="keyword">) {</span> <span class="comment">// For Firefox, Opera</span></code>
          						<code class="text-indent-3"><span class="variable">relatedTarget</span> = <span class="keyword">event</span>.<span class="attribute">relatedTarget</span>;</code>
          					<code class="text-indent-2"><span class="keyword">}</span></code>
          					<code class="text-indent-2"><span class="keyword">return</span> <span class="variable">relatedTarget</span>;</code>
          				<code class="text-indent-1"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="keyword">};</span></code>
          		</pre>
<div class="button-row clearfix">
<p class="button-style preview-icon"><a href="http://www.lingihuang.com/viviancrap/wp-content/uploads/dom_event_model_advanced/currentTarget_relatedTarget.html" target="_blank">View<span>Demo</span></a></p>
</p></div>
<h4 class="subtitle align-subtitle">Convert Mouse Coordinates</h4>
<pre>
          			<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">CSS</span> <span class="keyword">= {</span></code>
          				<code class="text-indent-1"><span class="comment">// Return the position of the element relative to the top-left corner of the document.</span></code>
          				<code class="text-indent-1"><span class="variable">offset</span><span class="keyword">: function(</span><span class="element">element</span><span class="keyword">) {</span></code>
          					<code class="text-indent-2"><span class="keyword">if (</span><span class="keyword">!</span><span class="element">element</span><span class="keyword">)</span> <span class="keyword">return;</span></code>
							<code class="text-indent-2"><span class="keyword">if (</span><span class="keyword">typeof</span> <span class="element">element</span> <span class="keyword">==</span> <span class="string">"string"</span><span class="keyword">)</span> <span class="element">element</span> <span class="keyword">=</span> <span class="element">document</span>.<span class="keyword">getElementById(</span><span class="element">element</span><span class="keyword">);</span></code>
          					<code class="text-indent-2"><span class="keyword">var</span> left = 0, top = 0;</code>
          					<code class="text-indent-2"><span class="keyword">for (</span><span class="keyword">var</span> <span class="variable">elem</span> <span class="keyword">=</span> <span class="element">element</span>; <span class="variable">elem</span>; <span class="variable">elem</span> <span class="keyword">=</span> <span class="variable">elem</span>.<span class="attribute">offsetParent</span><span class="keyword">) {</span></code>
          						<code class="text-indent-3"><span class="variable">left</span> <span class="keyword">+=</span> <span class="variable">elem</span>.<span class="attribute">offsetLeft</span>;</code>
          						<code class="text-indent-3"><span class="variable">top</span> <span class="keyword">+=</span> <span class="variable">elem</span>.<span class="attribute">offsetTop</span>;</code>
          					<code class="text-indent-2"><span class="keyword">}</span></code>
          					<code class="text-indent-2"><span class="keyword">for (</span><span class="variable">elem</span> <span class="keyword">=</span> <span class="element">element</span>.<span class="attribute">parentNode</span>; <span class="variable">elem</span> <span class="keyword">&#038;&#038;</span> <span class="variable">elem</span> <span class="keyword">!=</span> <span class="element">document.body</span>; <span class="variable">elem</span> <span class="keyword">=</span> <span class="element">elem</span>.<span class="attribute">parentNode</span><span class="keyword">) {</span></code>
          						<code class="text-indent-3"><span class="keyword">if (</span><span class="variable">elem</span>.<span class="attribute">scrollLeft</span><span class="keyword">)</span> <span class="variable">left</span> <span class="keyword">-=</span> <span class="variable">elem</span>.<span class="attribute">scrollLeft</span>;</code>
          						<code class="text-indent-3"><span class="keyword">if (</span><span class="variable">elem</span>.<span class="attribute">scrollTop</span><span class="keyword">)</span> <span class="variable">top</span> <span class="keyword">-=</span> <span class="variable">elem</span>.<span class="attribute">scrollTop</span>;</code>
          					<code class="text-indent-2"><span class="keyword">}</span></code>
          					<code class="text-indent-2"><span class="keyword">return</span> <span class="keyword">{</span><span class="attribute">left:</span> <span class="value">left</span>, <span class="attribute">top:</span> <span class="value">top</span><span class="keyword">}</span>;</code>
          				<code class="text-indent-1"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="keyword">};</span></code>
          		</pre>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Drag and Drop</div>
</p></div>
</p></div>
</p></div>
</p></div>
<p>drag() takes two arguments. The first is the element that is to be dragged. This may be the element on which the mousedown event occurred or a containing element (e.g., you might allow the user to drag on the titlebar of a window to move the entire window).<br />
          		In either cases, it muse refer to a document element that is absolutely positioned using the CSS position attribute. The second argument is the event object associated with the triggering mousedown event.</p>
<p>drag() records the position of the mousedown event and then registers event handlers for the mousemove and mouseup events that follow the mousedown event. The handler for the mousemove event is responsible for moving the document element, and the handler for the mouseup event is responsible for deregistering itself and the mousemove handler.<br />
          		It is important to note that the mousemove and mouseup handlers are registered as capturing event handlers because the user may move the mouse faster than the document element can follow it, and some of these events occur outside the original target element. Without capturing, the events may not be dispatched to the correct handlers.<br />
          		 Also, note that the moveHandler() and upHandler() functions that are registered to handle these events are defined as functions nested within drag(). Because they are defined in this nested scope they can use the arugments and local variables of drag(), which considerably simplifies their implementation.</p>
<div class="button-row clearfix">
<p class="button-style preview-icon"><a href="http://www.lingihuang.com/viviancrap/wp-content/uploads/dom_event_model_advanced/droppable.html" target="_blank">View<span>Demo</span></a></p>
</p></div>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Key Events</div>
</p></div>
</p></div>
</p></div>
</p></div>
<ul class="entry-list">
<li>onKeyDown: A keyboard key is pressed.</li>
<li>onKeyPress: A keyboard key is pressed or held down.</li>
<li>onKeyUp: A keyboard key is released.</li>
</ul>
<h4 class="subtitle align-subtitle">charCode and keyCode</h4>
<p>charCode is the integer property specifies for keydown and keyup events and Unicode character code for keypress events. Use String.fromCharCode() to convert character codes to strings.</p>
<div class="button-row clearfix">
<p class="button-style preview-icon"><a href="http://www.lingihuang.com/viviancrap/wp-content/uploads/dom_event_model_advanced/selectList.html" target="_blank">View<span>Demo</span></a></p>
</p></div>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Related Posts</div>
</p></div>
</p></div>
</p></div>
</p></div>
<ul class="entry-list">
<li><a href="http://www.brainjar.com/dhtml/events/default.asp" target="_blank">The DOM Event Model</a></li>
<li><a href="http://www.quirksmode.org/js/events_mouse.html#click" target="_blank">Mouse Events</a></li>
<li><a href="http://www.quirksmode.org/js/events_properties.html" target="_blank">Event Properties</a></li>
<li><a href="http://www.quirksmode.org/js/dragdrop.html" target="_blank">Drag and Drop</a></li>
<li><a href="https://developer.mozilla.org/en/DragDrop/Drag_and_Drop" target="_blank">Mozilla Drag and Drop Documentation</a></li>
<li><a href="http://www.webreference.com/programming/javascript/mk/column2/" target="_blank">How to Drag and Drop in Javascript</a></li>
<li><a href="http://ianjung1974.blogspot.com/2007/10/javascript.html" target="_blank">How to Drag and Drop in Javascript (Chinese)</a></li>
</ul></div>
]]></content:encoded>
			<wfw:commentRss>http://www.lingihuang.com/viviancrap/archives/the-dom-event-model-advanced/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript Error Handling</title>
		<link>http://www.lingihuang.com/viviancrap/archives/javascript-error-handling/</link>
		<comments>http://www.lingihuang.com/viviancrap/archives/javascript-error-handling/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 14:44:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.lingihuang.com/viviancrap/?p=169</guid>
		<description><![CDATA[




Types of Errors





Syntax Errors
Syntax errors are errors in grammar and punctuation such as mismatched quotes or missed commas. These errors are caught quickly if you have the browser&#8217;s built-in error detector in display mode or run the script through jsLint.
Runtime Errors
Runtime errors only show up as the script is executed. Common examples are calling a [...]]]></description>
			<content:encoded><![CDATA[<div class="entry-content">
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Types of Errors</div>
</p></div>
</p></div>
</p></div>
</p></div>
<ul class="entry-list">
<li>Syntax Errors</li>
<p>Syntax errors are errors in grammar and punctuation such as mismatched quotes or missed commas. These errors are caught quickly if you have the browser&#8217;s built-in error detector in display mode or run the script through jsLint.</p>
<li>Runtime Errors</li>
<p>Runtime errors only show up as the script is executed. Common examples are calling a function that hasn&#8217;t been declared (typing error or case-sensitivity issue) or division by zero. Although JavaScript is typeless, many built in objects expect and/or return specific types (eg. style.left needs string type).</p>
<li>Logic Errors</li>
<p>Logic errors are basic errors in the programmer&#8217;s algorithms or procedural errors. Diagnosis only comes when incorrect results occur and solution requires mapping out the flow for test cases. The wrong scoping of a variable is an example of this kind of error.</p>
<li>Incorrect Operator Precedence Errors</li>
<p>Incorrect operator precedence errors are basic mathematical grouping errors. The best way to avoid them is with brackets to force the order that you want operations to occur explicitly.</p>
<li>Browser Implementation Errors</li>
<p>Browser implementation errors are quirks that occur in one browser but not others. See Browser Issues for more details. Test your code on all anticipated client browsers!</p>
</ul>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Global Error Handling</div>
</p></div>
</p></div>
</p></div>
</p></div>
<p>The <span class="highlighted">window</span> object has an event called <span class="highlighted">onerror</span> that is invoked whenever there&#8217;s an unhandled error on the page.</p>
<p>As you can see, the event will pass 3 arguments to the invoked function. The first one is the actual <span class="highlighted">error message</span>. The second one is the <span class="highlighted">URL of the file</span> containing the error (useful if the error is in an external.js file.) The last argument is the <span class="highlighted">line number</span> in that file where the error happened.</p>
<p>Returning <span class="highlighted">true</span> tells the browser that you have taken care of the problem. If you return false instead, the browser will proceed to treat the error as unhandled, showing the error message and the status bar icon.</p>
<pre>
          			<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">onErrorHandler</span> <span class="keyword">= function(</span><span class="string">message</span>, <span class="string">fileName</span>, <span class="value">lineNumber</span><span class="keyword">) {</span></code>
          				<code class="text-indent-1">var element = document.createElement("p");</code>
          				<code class="text-indent-1">element.innerHTML = <span class="string">"Error: " + <span class="string">message</span> + "&lt;br /&gt;File Name: " + <span class="keyword">decodeURIComponent(</span><span class="string">fileName</span><span class="keyword">)</span> + "&lt;br /&gt;Line: " + <span class="value">lineNumber</span></span>;</code>
          				<code class="text-indent-1">document.body.appendChild(element);</code>
          				<code class="text-indent-1"><span class="comment">// Returning true tells the browser that you have taken care of the problem.</span></code>
          				<code class="text-indent-1"><span class="comment">// If you return false instead, the browser will proceed to treat the error as unhandled, showing the error message and the status bar icon.</span></code>
          				<code class="text-indent-1"><span class="keyword">return true</span>;</code>
          			<code class="text-indent-0"><span class="keyword">};</span></code>
          			<code class="text-indent-0"><span class="element">window</span>.<span class="keyword">onerror</span> = <span class="variable">onErrorHandler</span>;</code>
          		</pre>
<div class="button-row clearfix">
<p class="button-style preview-icon"><a href="http://www.lingihuang.com/viviancrap/wp-content/uploads/javascript_error_handling/global_error_handling.html" target="_blank">View<span>Demo</span></a></p>
</p></div>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Structured Error Handling</div>
</p></div>
</p></div>
</p></div>
</p></div>
<p>If anything goes wrong in the statements that are inside the <span class="highlighted">try</span> block&#8217;s statements then the statements in the <span class="highlighted">catch</span> block will be executed and the error will be passed in the error variable. The <span class="highlighted">finally</span> block is optional and, if present, is always executed last, regardless if there was an error caught or not.</p>
<p>The <span class="highlighted">error object</span> has two important properties: <span class="highlighted">name</span> and <span class="highlighted">message</span>. The message property contains the same error message that we have seen before. The name property contains the kind of error that happened and we can use that to decide if we know what to do with that error.</p>
<pre>
          			<code class="text-indent-0"><span class="keyword">function</span> <span class="variable">catchError()</span> <span class="keyword">{</span></code>
          				<code class="text-indent-1">var element = document.createElement("p");</code>
          				<code class="text-indent-1">var html = "";</code>
          				<code class="text-indent-1"><span class="keyword">try {</span></code>
          					<code class="text-indent-2">var username = <span class="element">window</span>.<span class="keyword">prompt(</span><span class="string">"Please Enter Your Name: "</span>, <span class="string">""</span><span class="keyword">)</span>;</code>
          					<code class="text-indent-2">html = "Your name (" + username + ") has " + username.length + " letters.";</code>
          				<code class="text-indent-1"><span class="keyword">} catch(<span class="variable">error</span>) {</span></code>
          					<code class="text-indent-2">if (<span class="variable">error</span>.<span class="attribute">name</span> <span class="keyword">==</span> <span class="value">"TypeError"</span>)</code>
          						<code class="text-indent-3">html = <span class="string"><span class="variable">error</span>.<span class="attribute">name</span> + ": " + <span class="variable">error</span>.<span class="attribute">message</span> + "&lt;br /&gt;File Name: " + <span class="keyword">decodeURIComponent(</span><span class="variable">error</span>.<span class="attribute">fileName</span><span class="keyword">)</span> + "&lt;br /&gt;Line: " + <span class="variable">error</span>.<span class="attribute">lineNumber</span></span>;</code>
          					<code class="text-indent-2">else</code>
          						<code class="text-indent-3"><span class="keyword">throw</span> <span class="variable">error</span>;</code>
          				<code class="text-indent-1"><span class="keyword">} finally {</span></code>
          					<code class="text-indent-2"><span class="comment">// The finally block is optional and, if present, is always executed last, regardless if there was an error caught or not.</span></code>
          					<code class="text-indent-2">element.innerHTML = html;</code>
          					<code class="text-indent-2">document.body.appendChild(element);</code>
          				<code class="text-indent-1"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="element">window</span>.<span class="keyword">onload</span> = <span class="variable">catchError</span>;</code>
          		</pre>
<div class="button-row clearfix">
<p class="button-style preview-icon"><a href="http://www.lingihuang.com/viviancrap/wp-content/uploads/javascript_error_handling/structured_error_handling.html" target="_blank">View<span>Demo</span></a></p>
</p></div>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Related Posts</div>
</p></div>
</p></div>
</p></div>
</p></div>
<ul class="entry-list">
<li><a href="http://www.learn-javascript-tutorial.com/ErrorHandlingAndDebugging.cfm" target="_blank">Error Handling and Debugging</a></li>
<li><a href="http://home.cogeco.ca/~ve3ll/jstutore.htm" target="_blank">Debugging</a></li>
</ul></div>
]]></content:encoded>
			<wfw:commentRss>http://www.lingihuang.com/viviancrap/archives/javascript-error-handling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cookie</title>
		<link>http://www.lingihuang.com/viviancrap/archives/cookie/</link>
		<comments>http://www.lingihuang.com/viviancrap/archives/cookie/#comments</comments>
		<pubDate>Tue, 26 May 2009 13:56:23 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.lingihuang.com/viviancrap/?p=162</guid>
		<description><![CDATA[




What are cookies?




          		
Cookies are small amounts of data stored by the web browser. They allow you to store particular information about a user and retrieve it every time they visit your pages. Each user has their own unique set of cookies.
Cookies are typically used by [...]]]></description>
			<content:encoded><![CDATA[<div class="entry-content">
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">What are cookies?</div>
</p></div>
</p></div>
</p></div>
</p></div>
<p>          		<img src="http://www.lingihuang.com/viviancrap/wp-content/uploads/2009/05/cookie.gif" alt="Cookie" title="Cookie" width="290" height="131" class="alignnone size-full wp-image-167 img-align-right" /></p>
<p>Cookies are small amounts of data stored by the web browser. They allow you to store particular information about a user and retrieve it every time they visit your pages. Each user has their own unique set of cookies.</p>
<p>Cookies are typically used by web servers to perform functions such as tracking your visits to websites, enabling you to log in to sites, and storing your shopping cart.</p>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">document.cookie</div>
</p></div>
</p></div>
</p></div>
</p></div>
<pre>
          			<code class="text-indent-0"><span class="element">document</span>.<span class="keyword">cookie</span> = <span class="string">"</span><span class="variable">name</span>=<span class="value">value</span>; <span class="variable">expires</span>=<span class="value">date</span>; <span class="variable">path</span>=<span class="value">path</span>; <span class="variable">domain</span>=<span class="value">domain</span>; <span class="variable">secure</span><span class="string">"</span>;</code>
          		</pre>
<div class="entry-table">
<div class="table-heading-row">
<div class="property-col">Property</div>
<div class="description-col">Description</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:20px;">name=value</div>
<div class="description-col" style="height:20px;">This sets both the cookie&#8217;s name and its value.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:86px;">expires=date</div>
<div class="description-col" style="height:86px;">This optional values sets the date that the cookie will expire on. The date should be in the format returned by the toGMTString() method of the Date object. If the expires value is not given, the cookie will be destroyed the moment the browser is closed.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:86px;">path=path</div>
<div class="description-col" style="height:86px;">This optional value specifies a path within the site to which the cookie applies. Only documents in this path will be able to retrieve the cookie. Usually this is left blank, meaning that only the path that set the cookie can retrieve it.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:86px;">domain=domain</div>
<div class="description-col" style="height:86px;">This optional value specifies a domain within which the cookie applies. Only websites in this domain will be able to retrieve the cookie. Usually this is left blank, meaning that only the domain that set the cookie can retrieve it.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:52px;">secure</div>
<div class="description-col" style="height:52px;">This optional flag indicates that the browser should use SSL when sending the cookie to the server. This flag is really used.</div>
</p></div>
</p></div>
<h4 class="subtitle align-subtitle">Set a Cookie</h4>
<pre>
          			<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">COOKIE</span> <span class="keyword">= {</span></code>
          				<code class="text-indent-1"><span class="variable">set</span><span class="keyword">: function(</span><span class="variable">name</span>, <span class="value">value</span>, <span class="variable">options</span>) {</code>
          					<code class="text-indent-2"><span class="keyword">if (</spapn><span class="keyword">!</span><span class="variable">name</span><span class="keyword">)</span> <span class="keyword">return</span>;</code>
          					<code class="text-indent-2"><span class="variable">options</span> = <span class="variable">options</span> <span class="keyword">|| {}</spn>;</code>
          					<code class="text-indent-2">if (<span class="keyword">!</span><span class="variable">options</span>.<span class="attribute">expires</span>) <span class="variable">options</span>.<span class="attribute">expires</span> = <span class="value">1</span>;</code>
          					<code class="text-indent-2"><span class="keyword">var</span> <span class="variable">date</span> = <span class="keyword">new Date()</span>;</code>
          					<code class="text-indent-2"><span class="variable">date</span>.<span class="keyword">setTime</span>(<span class="variable">date</span>.<span class="keyword">getTime()</span> <span class="keyword">+</span> <span class="variable">options</span>.<span class="attribute">expires</span><span class="value">*24*60*60*1000</span>);</code>
          					<code class="text-indent-2"><span class="keyword">var</span> <span class="variable">expires</span> = <span class="string">"; expires="</span> + <span class="variable">date</span>.<span class="keyword">toGMTString()</span>;</code>
          					<code class="text-indent-2"><span class="keyword">var</span> <span class="variable">path</span> = <span class="variable">options</span>.<span class="attribute">path</span> <span class="keyword">?</span> <span class="string">'; path='</span> <span class="keyword">+</span> (<span class="variable">options</span>.<span class="attribute">path</span>) <span class="keyword">:</span> <span class="string">''</span>;</code>
          					<code class="text-indent-2"><span class="keyword">var</span> <span class="variable">domain</span> = <span class="variable">options</span>.<span class="attribute">domain</span> <span class="keyword">?</span> <span class="string">'; domain='</span> + (<span class="variable">options</span>.<span class="attribute">domain</span>) <span class="keyword">:</span> <span class="string">''</span>;</code>
          					<code class="text-indent-2"><span class="keyword">var</span> <span class="variable">secure</span> = <span class="variable">options</span>.<span class="attribute">secure</span> <span class="keyword">?</span> <span class="string">'; secure'</span> <span class="keyword">:</span> <span class="string">''</span>;</code>
          					<code class="text-indent-2"><span class="element">document</span>.<span class="keyword">cookie</span> = <span class="keyword">[</span><span class="variable">name</span>, <span class="string">"="</span>, <span class="keyword">encodeURIComponent(</span><span class="value">value</span><span class="keyword">)</span>, <span class="variable">expires</span>, <span class="variable">path</span>, <span class="variable">domain</span>, <span class="variable">secure</span><span class="keyword">]</span>.<span class="keyword">join(</span><span class="string">""</span><span class="keyword">)</span>;</code>
          				<code class="text-indent-1"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="keyword">};</span></code>
          		</pre>
<p></p>
<h4 class="subtitle align-subtitle">Retrive a Cookie</h4>
<pre>
          			<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">COOKIE</span> <span class="keyword">= {</span></code>
          				<code class="text-indent-1"><span class="variable">get</span><span class="keyword">: function(</span><span class="variable">name</span><span class="keyword">) {</span></code>
          					<code class="text-indent-2"><span class="keyword">if (</spapn><span class="keyword">!</span><span class="variable">name</span><span class="keyword">)</span> <span class="keyword">return</span>;</code>
          					<code class="text-indent-2">var value = "";</code>
          					<code class="text-indent-2"><span class="keyword">if (</span><span class="element">document</span>.<span class="keyword">cookie</span> <span class="keyword">&#038;&#038;</span> <span class="element">document</span>.<span class="keyword">cookie</span> <span class="keyword">!=</span> <span class="string">""</span><span class="keyword">) {</span></code>
          						<code class="text-indent-3"><span class="keyword">var</span> <span class="variable">cookies</span> = <span class="element">document</span>.<span class="keyword">cookie</span>.<span class="keyword">split(</span><span class="string">";"</span><span class="keyword">)</span>;</code>
          						<code class="text-indent-3"><span class="keyword">for (</span><span class="keyword">var</span> <span class="variable">i</span><span class="keyword"> = </span><span class="value">0</span>; <span class="variable">i</span> <span class="keyword">&lt;</span> <span class="value">cookies.length</span>; <span class="variable">i</span><span class="keyword">++</span><span class="keyword">) {</span></code>
          							<code class="text-indent-4"><span class="keyword">var</span> <span class="variable">cookie</span> <span class="keyword">=</span> <span class="keyword">cookies[</span><span class="variable">i</span><span class="keyword">]</span>;</code>
          							<code class="text-indent-4"><span class="comment">// Trim the space.</span></code>
          							<code class="text-indent-4"><span class="keyword">while(</span><span class="variable">cookie</span>.<span class="keyword">charAt(</span><span class="value">0</span><span class="keyword">)</span> <span class="keyword">==</span> <span class="value">" "</span><span class="keyword">)</span> <span class="variable">cookie</span> = <span class="variable">cookie</span>.<span class="keyword">substring(</span><span class="value">1</span>, <span class="value">cookie.length</span><span class="keyword">)</span>;</code>
          							<code class="text-indent-4"><span class="comment">// Tell if this cookie string begins with the name we want.</span></code>
          							<code class="text-indent-4"><span class="keyword">if (</span><span class="variable">cookie</span>.<span class="keyword">substring(</span><span class="value">0</span>, <span class="value">name.length + 1</span><span class="keyword">)</span> <span class="keyword">== (</span><span class="variable">name</span> <span class="keyword">+</span> <span class="string">"="</span><span class="keyword">)</span><span class="keyword">) {</span></code>
          								<code class="text-indent-5"><span class="variable">value</span> = <span class="keyword">decodeURIComponent(</span><span class="variable">cookie</span>.<span class="keyword">substring(</span><span class="variable">name.length + 1</span><span class="keyword">))</span>;</code>
          								<code class="text-indent-5"><span class="keyword">break</span>;</code>
          							<code class="text-indent-4"><span class="keyword">}</span></code>
          						<code class="text-indent-3"><span class="keyword">}</span></code>
          					<code class="text-indent-2"><span class="keyword">}</span></code>
          					<code class="text-indent-2"><span class="keyword">return</span> <span class="variable">value</span>;</code>
          				<code class="text-indent-1"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="keyword">};</span></code>
          		</pre>
<p></p>
<h4 class="subtitle align-subtitle">Delete a Cookie</h4>
<pre>
          			<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">COOKIE</span> <span class="keyword">= {</span></code>
          				<code class="text-indent-1"><span class="variable">del</span><span class="keyword">: function(</span><span class="variable">name</span><span class="keyword">) {</span></code>
          					<code class="text-indent-2"><span class="keyword">if (</spapn><span class="keyword">!</span><span class="variable">name</span><span class="keyword">)</span> <span class="keyword">return</span>;</code>
          					<code class="text-indent-2"><span class="keyword">this</span>.<span class="keyword">set(</span><span class="variable">name</span>, <span class="string">""</span>, <span class="value">-1</span><span class="keyword">)</span>;</code>
          				<code class="text-indent-1"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="keyword">};</span></code>
          		</pre>
<div class="button-row clearfix">
<p class="button-style preview-icon"><a href="http://www.lingihuang.com/viviancrap/wp-content/uploads/cookie/index.html" target="_blank">View<span>Demo</span></a></p>
</p></div>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Related Posts</div>
</p></div>
</p></div>
</p></div>
</p></div>
<ul class="entry-list">
<li><a href="http://www.elated.com/articles/javascript-and-cookies/" target="_blank">Tutorial: JavaScript and Cookies</a></li>
<li><a href="http://www.quirksmode.org/js/cookies.html" target="_blank">Cookies</a></li>
</ul></div>
]]></content:encoded>
			<wfw:commentRss>http://www.lingihuang.com/viviancrap/archives/cookie/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The DOM Event Model Basic</title>
		<link>http://www.lingihuang.com/viviancrap/archives/the-dom-event-model-basic/</link>
		<comments>http://www.lingihuang.com/viviancrap/archives/the-dom-event-model-basic/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 09:00:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.lingihuang.com/viviancrap/?p=87</guid>
		<description><![CDATA[




Event Flow





          			
Event Capturing
Netscape maintained that the event on element1 takes place first. This is called event capturing. The event handler of element1 fires first, the event handler of element2 fires last.
&#160;
          			
Event Bubbling
Microsoft maintained that the [...]]]></description>
			<content:encoded><![CDATA[<div class="entry-content">
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Event Flow</div>
</p></div>
</p></div>
</p></div>
</p></div>
<ul class="entry-list">
          			<img class="img-align-right" src="http://www.lingihuang.com/viviancrap/wp-content/uploads/2009/04/javascript-event-handler-1.gif" alt="Javascript Event Handler" title="Javascript Event Handler" width="220" height="133" class="alignnone size-full wp-image-89" /></p>
<li>Event Capturing</li>
<p>Netscape maintained that the event on element1 takes place first. This is called <span class="highlighted">event capturing</span>. The event handler of element1 fires first, the event handler of element2 fires last.</p>
<p>&nbsp;</p>
<p>          			<img class="img-align-right" src="http://www.lingihuang.com/viviancrap/wp-content/uploads/2009/04/javascript-event-handler-2.gif" alt="Javascript Event Handler" title="Javascript Event Handler" width="220" height="133" class="alignnone size-full wp-image-90" /></p>
<li>Event Bubbling</li>
<p>Microsoft maintained that the event on element2 takes precedence. This is called <span class="highlighted">event bubbling</span>. The event handler of element2 fires first, the event handler of element1 fires last.</p>
<p>&nbsp;</p>
<p>          			<img class="img-align-right" src="http://www.lingihuang.com/viviancrap/wp-content/uploads/2009/04/javascript-event-handler-3.gif" alt="Javascript Eevent Handler" title="Javascript Eevent Handler" width="220" height="133" class="alignnone size-full wp-image-91" /></p>
<li>W3C Event Model</li>
<p>W3C has very sensibly decided to take a middle position in this struggle. Any event taking place in the W3C event model is first captured until it reaches the target element and then bubbles up again.</p>
</ul>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">The Event Object</div>
</p></div>
</p></div>
</p></div>
</p></div>
<div class="entry-table">
<div class="table-heading-row">
<div class="property-col">Property Name</div>
<div class="description-col">Description</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:70px;">type</div>
<div class="description-col" style="height:70px;">The type of event that occurred. The value of this property is the name of the event type and is the same string value that was used when registering the event handler (e.g., &#8220;click&#8221; or &#8220;mouseover&#8221;).</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:36px;">target</div>
<div class="description-col" style="height:36px;">The node on which the event occurred, which may not be the same as currentTarget.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:88px;">currentTarget</div>
<div class="description-col" style="height:88px;">The node on which the event is currently being processed. If the event is being processed during the capturing or bubbling phase of propagation, the value of this property is different from the value of the target property.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:70px;">eventPhase</div>
<div class="description-col" style="height:70px;">A number that specifies what phase of event propagation is currently in process. The value of one of the constants Event.CAPTURING_PHASE, EVENT.AT_TARGET, or EVENT_BUBBLING_PHASE.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:20px;">timeStamp</div>
<div class="description-col" style="height:20px;">A Date object that specifies when the event occurred.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:36px;">bubbles</div>
<div class="description-col" style="height:36px;">A boolean that specifies whether this event (and events of this type) bubbles up the document tree.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:54px;">cancelable</div>
<div class="description-col" style="height:54px;">A boolean that specifies whether the event has a default action associated with it that can be canceled with the preventDefault() method.</div>
</p></div>
</p></div>
<div class="entry-table">
<div class="table-heading-row">
<div class="property-col">Method Name</div>
<div class="description-col">Description</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:54px;">stopPropagation()</div>
<div class="description-col" style="height:54px;">Any event handler can call stopPropagation() to prevent the event from being propagated beyond the node at which it is currently being handled.</div>
</p></div>
<div class="table-row">
<div class="property-col" style="height:54px;">preventDefault()</div>
<div class="description-col" style="height:54px;">Any event handler can call preventDefault() to prevent the browser from performing a default action associated with the event.</div>
</p></div>
</p></div>
<div class="entry-table">
<div class="table-heading-row">
<div class="w3c-col">W3C Standard</div>
<div class="ie-col">Internet Explorer</div>
<div class="desc-col">Description</div>
</p></div>
<div class="table-row">
<div class="w3c-col" style="height:36px;">type</div>
<div class="ie-col" style="height:36px;">type</div>
<div class="desc-col" style="height:36px;">A string that specifies the type of event that occurred.</div>
</p></div>
<div class="table-row">
<div class="w3c-col" style="height:36px;">target</div>
<div class="ie-col" style="height:36px;">srcElement</div>
<div class="desc-col" style="height:36px;">The document element on which the event occurred.</div>
</p></div>
<div class="table-row">
<div class="w3c-col" style="height:20px;">currentTarget</div>
<div class="ie-col" style="height:20px;">none</div>
<div class="desc-col" style="height:20px;">Not applicable in IE.</div>
</p></div>
<div class="table-row">
<div class="w3c-col" style="height:20px;">eventPhase</div>
<div class="ie-col" style="height:20px;">none</div>
<div class="desc-col" style="height:20px;">Not applicable in IE.</div>
</p></div>
<div class="table-row">
<div class="w3c-col" style="height:20px;">timeStamp</div>
<div class="ie-col" style="height:20px;">none</div>
<div class="desc-col" style="height:20px;">Not applicable in IE.</div>
</p></div>
<div class="table-row">
<div class="w3c-col" style="height:70px;">stopPropagation()</div>
<div class="ie-col" style="height:70px;">cancelBubble</div>
<div class="desc-col" style="height:70px;">A boolean property that, when set to true, prevents the current event from bubbling any further up the element containment hierarchy.</div>
</p></div>
<div class="table-row">
<div class="w3c-col" style="height:70px;">preventDefault()</div>
<div class="ie-col" style="height:70px;">returnValue</div>
<div class="desc-col" style="height:70px;">A boolean property that can be set to false to prevent the browser from performing the default action associated with the event.</div>
</p></div>
</p></div>
<h4 class="subtitle">The IE Event Object as a Global Variable</h4>
<pre>
          			<code class="text-indent-0"><span class="keyword">function</span> <span class="variable">onMouseDownHandler</span><span class="keyword">(</span><span class="variable">event</span><span class="keyword">) {</span></code>
          				<code class="text-indent-1"><span class="keyword">var</span> <span class="variable">event</span> = <span class="variable">event</span> <span class="keyword">||</span> <span class="element">window</span>.<span class="keyword">event</span>; <span class="comment">// IE stores events in window object.</span></code>
          			<code class="text-indent-0"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="element">element</span>.<span class="keyword">onmousedown</span> = <span class="variable">onMouseDownHandler</span>;</code>
          		</pre>
<h4 class="subtitle align-subtitle">currentTarget and relatedTarget/fromElement and toElement</h4>
<pre>
          			<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">GENERIC</span> <span class="keyword">= {</span></code>
          				<code class="text-indent-1"><span class="variable">findCurrentTarget</span><span class="keyword">: function(</span><span class="keyword">event</span>, <span class="element">node</span><span class="keyword">) {</span></code>
          					<code class="text-indent-2"><span class="keyword">var</span> <span class="variable">currentTarget</span>;</code>
          					<code class="text-indent-2"><span class="keyword">if (</span><span class="element">window</span>.<span class="keyword">event</span><span class="keyword">)</span> <span class="comment">// For IE</span></code>
          						<code class="text-indent-3"><span class="variable">currentTarget</span> = <span class="element">node</span>;</code>
          					<code class="text-indent-2"><span class="keyword">else if (</span><span class="keyword">event</span> <span class="keyword">&#038;&#038;</span> <span class="keyword">event</span>.<span class="attribute">currentTarget</span><span class="keyword">)</span> <span class="comment">// For Firefox, Opera</span></code>
          						<code class="text-indent-3"><span class="variable">currentTarget</span> = <span class="keyword">event</span>.<span class="attribute">currentTarget</span>;</code>
          					<code class="text-indent-2"><span class="keyword">return</span> <span class="variable">currentTarget</span>;</code>
          				<code class="text-indent-1"><span class="keyword">},</span></code>
          				<code class="text-indent-1"><span class="variable">findRelatedTarget</span><span class="keyword">: function(event) {</span></code>
          					<code class="text-indent-2"><span class="keyword">var</span> <span class="variable">relatedTarget</span>;</code>
          					<code class="text-indent-2"><span class="keyword">if (</span><span class="element">window</span>.<span class="keyword">event</span><span class="keyword">) {</span> <span class="comment">// For IE</span></code>
          						<code class="text-indent-3"><span class="keyword">if (</span><span class="element">window</span>.<span class="keyword">event</span>.<span class="attribute">fromElement</span><span class="keyword">)</span></code>
          							<code class="text-indent-4"><span class="variable">relatedTarget</span> = <span class="element">window</span>.<span class="keyword">event</span>.<span class="attribute">fromElement</span>;</code>
          						<code class="text-indent-3"><span class="keyword">else if (</span><span class="element">window</span>.<span class="keyword">event</span>.<span class="attribute">toElement</span><span class="keyword">)</span></code>
          							<code class="text-indent-4"><span class="variable">relatedTarget</span> = <span class="element">window</span>.<span class="keyword">event</span>.<span class="attribute">toElement</span>;</code>
          					<code class="text-indent-2"><span class="keyword">} else if (</span><span class="keyword">event</span> <span class="keyword">&#038;&#038;</span> <span class="keyword">event</span>.<span class="attribute">relatedTarget</span><span class="keyword">) {</span> <span class="comment">// For Firefox, Opera</span></code>
          						<code class="text-indent-3"><span class="variable">relatedTarget</span> = <span class="keyword">event</span>.<span class="attribute">relatedTarget</span>;</code>
          					<code class="text-indent-2"><span class="keyword">}</span></code>
          					<code class="text-indent-2"><span class="keyword">return</span> <span class="variable">relatedTarget</span>;</code>
          				<code class="text-indent-1"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="keyword">};</span></code>
          		</pre>
<div class="button-row clearfix">
<p class="button-style preview-icon"><a href="http://www.lingihuang.com/viviancrap/wp-content/uploads/dom_event_model_advanced/currentTarget_relatedTarget.html" target="_blank">View<span>Demo</span></a></p>
</p></div>
<h4 class="subtitle align-subtitle">Turn all capturing and bubbling off</h4>
<pre>
					<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">GENERIC</span> <span class="keyword">= {</span></code>
						<code class="text-indent-1"><span class="variable">stopPropagation</span><span class="keyword">: function(</span><span class="keyword">event</span><span class="keyword">) {</span></code>
							<code class="text-indent-2"><span class="keyword">if (</span><span class="element">window</span>.<span class="keyword">event</span><span class="keyword">)</span> <span class="comment">// For IE</span></code>
								<code class="text-indent-3"><span class="element">window</span>.<span class="keyword">event</span>.<span class="attribute">cancelBubble</span> = <span class="value">true</span>;</span></code>
							<code class="text-indent-2"><span class="keyword">if (</span><span class="keyword">event</span> <span class="keyword">&#038;&#038;</span> <span class="keyword">event</span>.<span class="attribute">stopPropagation</span><span class="keyword">)</span> <span class="comment">// For Modern Browsers</span></code>
								<code class="text-indent-3"><span class="keyword">event</span>.<span class="keyword">stopPropagation()</span>;</span></code>
						<code class="text-indent-1"><span class="keyword">}</span></code>
					<code class="text-indent-0"><span class="keyword">};</span></code>
				</pre>
<h4 class="subtitle align-subtitle">Prevent Default Action</h4>
<pre>
					<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">GENERIC</span> <span class="keyword">= {</span></code>
						<code class="text-indent-1"><span class="variable">cancelDefault</span><span class="keyword">: function(</span><span class="keyword">event</span><span class="keyword">) {</span></code>
							<code class="text-indent-2"><span class="keyword">if (</span><span class="element">window</span>.<span class="keyword">event</span><span class="keyword">)</span> <span class="comment">// For IE</span></code>
								<code class="text-indent-3"><span class="element">window</span>.<span class="keyword">event</span>.<span class="attribute">returnValue</span> = <span class="value">false</span>;</span></code>
							<code class="text-indent-2"><span class="keyword">if (</span><span class="keyword">event</span> <span class="keyword">&#038;&#038;</span> <span class="keyword">event</span>.<span class="attribute">preventDefault</span><span class="keyword">)</span> <span class="comment">// For Modern Browsers</span></code>
								<code class="text-indent-3"><span class="keyword">event</span>.<span class="keyword">preventDefault()</span>;</span></code>
						<code class="text-indent-1"><span class="keyword">}</span></code>
					<code class="text-indent-0"><span class="keyword">};</span></code>
				</pre>
<div class="button-row clearfix">
<p class="button-style preview-icon"><a href="http://www.lingihuang.com/viviancrap/wp-content/uploads/dom_event_model_basic/contextMenu.html" target="_blank">View<span>Demo</span></a></p>
</p></div>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Event Handler Registration</div>
</p></div>
</p></div>
</p></div>
</p></div>
<ul class="entry-list">
<li>Example One</li>
<pre>
          				<code class="text-indent-0"><span class="comment">// Add Event Handlers to The Element</span></code>
          				<code class="text-indent-0">element.onclick = doSomething;</code>
          			</pre>
<pre>
          				<code class="text-indent-0"><span class="comment">// Remove Event Handlers</span></code>
          				<code class="text-indent-0">element.onclick = <span class="keyword">null</span>;</code>
          			</pre>
<li>Example Two: attachEvent() and addEventListener()</li>
<pre>
          				<code class="text-indent-0"><span class="comment">// Add Event Handlers to The Element</span></code>
          				<code class="text-indent-0"><span class="keyword">if (</span><span class="element">element</span>.<span class="keyword">attachEvent</span><span class="keyword">) {</span> <span class="comment">// For IE</span></code>
          					<code class="text-indent-1"><span class="element">element</span>.<span class="keyword">attachEvent(</span><span class="string">"onclick"</span>, <span class="variable">doSomething</span><span class="keyword">)</span>;</code>
          				<code class="text-indent-0"><span class="keyword">} else if (</span><span class="element">element</span>.<span class="keyword">addListener</span><span class="keyword">) {</span> <span class="comment">// For Modern Browsers</span></code>
          					<code class="text-indent-1"><span class="element">element</span>.<span class="keyword">addEventListener(</span><span class="string">"click"</span>, <span class="variable">doSomething</span>, <span class="value">false</span><span class="keyword">)</span>;</code>
          				<code class="text-indent-0"><span class="keyword">}</span></code>
          			</pre>
<pre>
          				<code class="text-indent-0"><span class="comment">// Remove Event Handlers</span></code>
          				<code class="text-indent-0"><span class="keyword">if (</span><span class="element">element</span>.<span class="keyword">detachEvent</span><span class="keyword">) {</span> <span class="comment">// For IE</span></code>
          					<code class="text-indent-1"><span class="element">element</span>.<span class="keyword">detachEvent(</span><span class="string">"onclick"</span>, <span class="variable">doSomething</span></span>)</span>;</code>
          				<code class="text-indent-0"><span class="keyword">} else if (</span><span class="element">element</span>.<span class="keyword">removeListener</span><span class="keyword">) {</span> <span class="comment">// For Modern Browsers</span></code>
          					<code class="text-indent-1"><span class="element">element</span>.<span class="keyword">removeEventListener(</span><span class="string">"click"</span>, <span class="variable">doSomething</span>, <span class="value">false</span><span class="keyword">)</span>;</code>
          				<code class="text-indent-0"><span class="keyword">}</span></code>
          			</pre>
<h4 class="subtitle align-subtitle">attachEvent() and the this keyword</h4>
<p>Event handlers registered with attachEvent() are invoked as global functions instead of as methods of the element on which they are registered. This means that the <span class="highlighted">this</span> keyword refers to <span class="highlighted">the global window object</span>. It is compounded, however, by the fact that the IE event object has no equivalent to the DOM <span class="highlighted">currentTarget</span> property. <span class="highlighted">srcElement</span> specifies the element that generated the event, but if the event has bubbled, this may be different from the element that is handling the event.</p>
<h4 class="subtitle align-subtitle">Arguments to addEventListener()</h4>
<p>The final argument to addEventListener() is a boolean value. If <span class="highlighted">true</span>, the specified event handler captures events during the capturing phase of event propagation. If the argument is <span class="highlighted">false</span>, the event handler is a normal event handler and is triggered when the event directly on the object or on a descendant of the element and subsequently bubbles up to the element.</p>
<h4 class="subtitle align-subtitle">addEventListener() and the this keyword</h4>
<p>When a function is registered as an event handler for a document element, it becomes a method of that document element. When the event handler is invoked, it is invoked as a method of the element, and, within the function, the <span class="highlighted">this</span> keyword refers to the element on which the event occurred.</p>
<li>Example Three: Register Objects as Event Handlers</li>
<pre>
          				<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">GENERIC</span> <span class="keyword">= {</span></code>
          					<code class="text-indent-1"><span class="comment">// Fix the bug the "this" keyword refers to the global window object when event handlers registered with attachEvent() are invoked as global functions instead of as methods of the element on which they are registered.</span></code>
          					<code class="text-indent-1"><span class="variable">addEvent</span><span class="keyword">: function(</span><span class="element">obj</span>, <span class="string">type</span>, <span class="variable">fn</span><span class="keyword"><span class="keyword">) {</span></span></code>
          						<code class="text-indent-2"><span class="keyword">if (</span><span class="keyword">!</span><span class="element">obj</span><span class="keyword">)</span> <span class="keyword">return</span>;</code>
          						<code class="text-indent-2"><span class="keyword">if (</span><span class="element">obj</span>.<span class="keyword">attachEvent</span>) { <span class="comment">// For IE</span></code>
          							<code class="text-indent-3"><span class="element">obj</span><span class="keyword">[</span><span class="string">"e"+type+fn</span><span class="keyword">]</span> = <span class="variable">fn</span>;</code>
          							<code class="text-indent-3"><span class="element">obj</span><span class="keyword">[</span><span class="string">type+fn</span><span class="keyword">]</span> = <span class="keyword">function(){</span><span class="element">obj</span><span class="keyword">[</span><span class="string">"e"+type+fn</span><span class="keyword">]</span><span class="keyword">(</span><span class="element">window</span>.<span class="keyword">event</span><span class="keyword">);}</span></code>
          							<code class="text-indent-3"><span class="element">obj</span>.<span class="keyword">attachEvent(</span><span class="string">"on"+type</span>, <span class="element">obj</span><span class="keyword">[</span><span class="string">type+fn</span><span class="keyword">])</span>;</code>
          						<code class="text-indent-2"><span class="keyword">} else if (</span><span class="element">obj</span>.<span class="keyword">addEventListener</span><span class="keyword">) {</span> <span class="comment">// For Modern Browsers</span></code>
          							<code class="text-indent-3"><span class="element">obj</span>.<span class="keyword">addEventListener(</span><span class="string">type</span>, <span class="variable">fn</span>, <span class="value">false</span><span class="keyword">)</span>;</code>
          						<code class="text-indent-2"><span class="keyword">}</span></code>
          					<code class="text-indent-1"><span class="keyword">},</span></code>
          					<code class="text-indent-1"><span class="variable">removeEvent</span><span class="keyword">: function(</span><span class="element">obj</span>, <span class="string">type</span>, <span class="variable">fn</span><span class="keyword">) {</span></code>
          						<code class="text-indent-2"><span class="keyword">if (</span><span class="keyword">!</span><span class="element">obj</span><span class="keyword">)</span> <span class="keyword">return</span>;</code>
          						<code class="text-indent-2"><span class="keyword">if (</span><span class="element">obj</span>.<span class="keyword">detachEvent</span><span class="keyword">) {</span> <span class="comment">// For IE</span></code>
          							<code class="text-indent-3"><span class="element">obj</span><span class="keyword">[</span><span class="string">"e"+type+fn</span><span class="keyword">]</span> = <span class="variable">fn</span>;</code>
          							<code class="text-indent-3"><span class="element">obj</span><span class="keyword">[</span><span class="string">type+fn</span><span class="keyword">]</span> = <span class="keyword">function(){</span><span class="element">obj</span><span class="keyword">[</span><span class="string">"e"+type+fn</span><span class="keyword">]</span><span class="keyword">(</span><span class="element">window</span>.<span class="keyword">event</span><span class="keyword">);}</span></code>
          							<code class="text-indent-3"><span class="element">obj</span>.<span class="keyword">detachEvent(</span><span class="string">"on"+type</span>, <span class="element">obj</span><span class="keyword">[</span><span class="string">type+fn</span><span class="keyword">])</span>;</code>
          						<code class="text-indent-2"><span class="keyword">} else if (</span><span class="element">obj</span>.<span class="keyword">removeEventListener</span><span class="keyword">) {</span> <span class="comment">// For Modern Browsers</span></code>
          							<code class="text-indent-3"><span class="element">obj</span>.<span class="keyword">removeEventListener(</span><span class="string">type</span>, <span class="variable">fn</span>, <span class="value">false</span><span class="keyword">)</span>;</code>
          						<code class="text-indent-2"><span class="keyword">}</span></code>
          					<code class="text-indent-1"><span class="keyword">}</span></code>
          				<code class="text-indent-0"><span class="keyword">};</span></code>
          			</pre>
<pre>
          				<code class="text-indent-0"><span class="keyword">function</span> <span class="variable">changeBackground</span><span class="keyword">() {</span></code>
          					<code class="text-indent-1"><span class="keyword">this</span>.style.backgroundColor = "#000"; <span class="comment">// "this" refers to the element</span></code>
          				<code class="text-indent-0"><span class="keyword">}</span></code>
          				<code class="text-indent-0"><span class="variable">GENERIC</span>.<span class="keyword">addEvent(</span><span class="element">element</span>, <span class="string">"click"</span>, <span class="variable">changeBackground</span><span class="keyword">)</span>;</code>
          				<code class="text-indent-0"><span class="variable">GENERIC</span>.<span class="keyword">removeEvent(</span><span class="element">element</span>, <span class="string">"click"</span>, <span class="variable">changeBackground</span><span class="keyword">)</span>;</code>
          			</pre>
</ul>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Related Posts</div>
</p></div>
</p></div>
</p></div>
</p></div>
<ul class="entry-list">
<li><a href="http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html" target="_blank">W3C Document Object Model Events</a></li>
<li><a href="http://www.brainjar.com/dhtml/events/default.asp" target="_blank">The DOM Event Model</a></li>
<li><a href="http://www.quirksmode.org/js/events_tradmod.html" target="_blank">Traditional Event Registration Model</a></li>
<li><a href="http://www.quirksmode.org/js/events_advanced.html" target="_blank">Advanced Event Registration Model</a></li>
<li><a href="http://www.quirksmode.org/js/events_order.html" target="_blank">Event Order</a></li>
</ul></div>
]]></content:encoded>
			<wfw:commentRss>http://www.lingihuang.com/viviancrap/archives/the-dom-event-model-basic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamically Load Style</title>
		<link>http://www.lingihuang.com/viviancrap/archives/dynamically-load-style/</link>
		<comments>http://www.lingihuang.com/viviancrap/archives/dynamically-load-style/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 14:36:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.lingihuang.com/viviancrap/?p=82</guid>
		<description><![CDATA[




Dynamic Style Loading





          			var HTTPUTILITY =  {
						getStyle: function(url, callback) {
							var isLoaded = false;
							var style = document.createElement("link");
							style.setAttribute("rel", "stylesheet");
							style.setAttribute("type", "text/css");
							style.setAttribute("href", url);
							style.onload = style.onreadystatechange = function() {
								if (!isLoaded &#038;&#038; (!this.readyState &#124;&#124; this.readyState == "loaded" &#124;&#124; this.readyState == "complete")) {
									isLoaded = true;
									if (typeof callback === "function")
										callback();
									if (this.tagName.toLowerCase() == "link")
										document.getElementsByTagName("head")[0].removeChild(this);
								}
							}
							var [...]]]></description>
			<content:encoded><![CDATA[<div class="entry-content">
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Dynamic Style Loading</div>
</p></div>
</p></div>
</p></div>
</p></div>
<pre>
          			<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">HTTPUTILITY</span> <span class="keyword">=  {</span></code>
						<code class="text-indent-1"><span class="variable">getStyle</span><span class="keyword">: function(</span><span class="string">url</span>, <span class="variable">callback</span><span class="keyword">) {</span></code>
							<code class="text-indent-2"><span class="keyword">var</span> <span class="variable">isLoaded</span> = <span class="value">false</span>;</code>
							<code class="text-indent-2"><span class="keyword">var</span> <span class="element">style</span> = <span class="element">document.</span><span class="keyword">createElement(</span><span class="string">"link"</span><span class="keyword">)</span>;</code>
							<code class="text-indent-2"><span class="element">style</span>.<span class="keyword">setAttribute(</span><span class="attribute">"rel"</span>, <span class="value">"stylesheet"</span><span class="keyword">)</span>;</code>
							<code class="text-indent-2"><span class="element">style</span>.<span class="keyword">setAttribute(</span><span class="attribute">"type"</span>, <span class="value">"text/css"</span><span class="keyword">)</span>;</code>
							<code class="text-indent-2"><span class="element">style</span>.<span class="keyword">setAttribute(</span><span class="attribute">"href"</span>, <span class="value">url</span><span class="keyword">)</span>;</code>
							<code class="text-indent-2"><span class="element">style</span>.<span class="keyword">onload</span> = <span class="element">style.</span><span class="keyword">onreadystatechange</span> = <span class="keyword">function() {</span></code>
								<code class="text-indent-3"><span class="keyword">if (</span><span class="keyword">!</span><span class="variable">isLoaded</span> <span class="keyword">&#038;&#038;</span> (<span class="keyword">!</span><span class="keyword">this</span>.<span class="attribute">readyState</span> <span class="keyword">||</span> <span class="keyword">this</span>.<span class="attribute">readyState</span> <span class="keyword">==</span> <span class="string">"loaded"</span> <span class="keyword">||</span> <span class="keyword">this</span>.<span class="attribute">readyState</span> <span class="keyword">==</span> <span class="string">"complete"</span><span class="keyword">)) {</span></code>
									<code class="text-indent-4"><span class="variable">isLoaded</span> = <span class="value">true</span>;</code>
									<code class="text-indent-4"><span class="keyword">if (</span><span class="keyword">typeof</span> <span class="variable">callback</span> <span class="keyword">===</span> <span class="string">"function"</span><span class="keyword">)</span></code>
										<code class="text-indent-5"><span class="variable">callback()</span>;</code>
									<code class="text-indent-4"><span class="keyword">if (</span><span class="keyword">this.tagName.toLowerCase()</span> <span class="keyword">==</span> <span class="string">"link"</span><span class="keyword">)</span></code>
										<code class="text-indent-5"><span class="keyword">document.getElementsByTagName(</span><span class="string">"head"</span>)<span class="value">[0]</span>.<span class="keyword">removeChild(</span><span class="keyword">this</span><span class="keyword">)</span>;</code>
								<code class="text-indent-3"><span class="keyword">}</span></code>
							<code class="text-indent-2"><span class="keyword">}</span></code>
							<code class="text-indent-2"><span class="keyword">var</span> <span class="element">head</span> = <span class="element">document</span>.<span class="keyword">getElementsByTagName(</span><span class="string">"head"</span><span class="keyword">)</span><span class="value">[0]</span>;</code>
							<code class="text-indent-2"><span class="element">head</span>.<span class="keyword">appendChild(</span><span class="element">style</span><span class="keyword">)</span>;</code>
						<code class="text-indent-1"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="keyword">};</span></code>
          		</pre>
<pre>
          			<code class="text-indent-0">function complete() {</code>
          				<code class="text-indent-1">alert("complete");</code>
          			<code class="text-indent-0">}</code>
          			<code class="text-indent-0"><span class="keyword">HTTPUTILITY.getStyle(</span><span class="string">"myStyle.css"</span>, <span class="keyword">function(){</span><span class="variable">complete()</span>;<span class="keyword">})</span>;</code>
          		</pre>
<div class="button-row clearfix">
<p class="button-style preview-icon"><a href="http://www.lingihuang.com/viviancrap/wp-content/uploads/dynamically_load_style/index.html" target="_blank">View<span>Demo</span></a></p>
</p></div>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Related Posts</div>
</p></div>
</p></div>
</p></div>
</p></div>
<ul class="entry-list">
<li><a href="http://www.phpied.com/javascript-include-ready-onload/" target="_blank">CSS Inclusion</a></li>
</ul></div>
]]></content:encoded>
			<wfw:commentRss>http://www.lingihuang.com/viviancrap/archives/dynamically-load-style/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamically Load Script</title>
		<link>http://www.lingihuang.com/viviancrap/archives/dynamically-load-script/</link>
		<comments>http://www.lingihuang.com/viviancrap/archives/dynamically-load-script/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 14:21:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.lingihuang.com/viviancrap/?p=77</guid>
		<description><![CDATA[




Comparison with XMLHttpRequest()




Advantages

Can request a file from anywhere on the net, not just the server your page was loaded from.
Works in IE even when ActiveX is turned off (though not when Javascript is turned off).
Works with a few older browsers that don&#8217;t support XMLHttpRequest, like Opera 7 (though not Macintosh versions of IE).

Disadvantages

Returned data has [...]]]></description>
			<content:encoded><![CDATA[<div class="entry-content">
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Comparison with XMLHttpRequest()</div>
</p></div>
</p></div>
</p></div>
</p></div>
<h4 class="subtitle align-subtitle">Advantages</h4>
<ul class="entry-list">
<li>Can request a file from anywhere on the net, not just the server your page was loaded from.</li>
<li>Works in IE even when ActiveX is turned off (though not when Javascript is turned off).</li>
<li>Works with a few older browsers that don&#8217;t support XMLHttpRequest, like Opera 7 (though not Macintosh versions of IE).</li>
</ul>
<h4 class="subtitle align-subtitle">Disadvantages</h4>
<ul class="entry-list">
<li>Returned data has to be formatted as <span class="highlighted">Javascript code</span>. XMLHttpRequest() can be used to fetch data in any format, XML, JSON, plain text, or whatever.</li>
<li>Can only do <span class="highlighted">GET</span> requests, not POST requests.</li>
<li>Whether the request is synchronous or asynchronous is pot luck, depending on the browser. With XMLHttpRequest() you can control this.</li>
<li>When fetching JSON data from an untrusted source, there is no possiblity of checking the data before feeding it to the Javascript parser. With XMLHttpRequest() you can parse the data with something like json2.js instead of eval() for secure parsing.</li>
</ul>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Load Only Once</div>
</p></div>
</p></div>
</p></div>
</p></div>
<pre>
					<code class="text-indent-0"><span class="keyword">var</span> <span class="element">script</span> = <span class="element">document.</span><span class="keyword">createElement(</span><span class="string">"script"</span><span class="keyword">)</span>;</code>
					<code class="text-indent-0"><span class="element">script.</span><span class="keyword">setAttribute(</span><span class="attribute">"language"</span>, <span class="value">"javascript"</span><span class="keyword">)</span>;</code>
					<code class="text-indent-0"><span class="element">script.</span><span class="keyword">setAttribute(</span><span class="attribute">"type"</span>, <span class="value">"text/javascript"</span><span class="keyword">)</span>;</code>
					<code class="text-indent-0"><span class="element">script.</span><span class="keyword">setAttribute(</span><span class="attribute">"src"</span>, <span class="value">url</span><span class="keyword">)</span>;</code>
					<code class="text-indent-0"><span class="keyword">var</span> <span class="element">head</span> = <span class="element">document</span>.<span class="keyword">getElementsByTagName(</span><span class="string">"head"</span><span class="keyword">)</span><span class="value">[0]</span>;</code>
					<code class="text-indent-0"><span class="element">head</span>.<span class="keyword">appendChild(</span><span class="element">script</span><span class="keyword">)</span>;</code>
          		</pre>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Multiple Loads</div>
</p></div>
</p></div>
</p></div>
</p></div>
<ul class="entry-list">
<li>Cache</li>
<p>First, you need to worry about <span class="highlighted">caching</span>. Typically, the url you&#8217;re going to be loading is going to be some kind of CGI program. After all, if the output doesn&#8217;t vary, why bother loading it more than once? Usually browsers are fairly smart about not caching those, but you might want to take some steps to ensure that the second call doesn&#8217;t just get you the cached copy of the result of the first call. So the CGI should probably be outputting headers to suppress caching. You might also put extra arguments on the URL, perhaps by keeping a count of the number of times you&#8217;ve called it, and adding a &#8220;?count=7&#8243; argument onto the end of the URL (where 7 is the current count). This makes the URL different on each call, and further ensures that you won&#8217;t get a cached copy.</p>
<li>Delete Script</li>
<p>Second, you&#8217;ll tend to accumulate a lot of &lt;script&gt; tags in your header. This doesn&#8217;t necessarily do a whole lot of harm, but it seems sloppy. So you could probably delete them by doing <span class="highlighted">head.removeChild(script)</span>. I think you can actually have the callback function do this immediately after loading. Removing the &lt;script&gt; tag does not undefine functions or variables that were defined by it, so you are done with it the moment it is done loading.</p>
</ul>
<h4 class="subtitle align-subtitle">Detect Load Completion</h4>
<p>After we have loaded the helper code, we obviously want to call it to start it running. However, we can&#8217;t just put a call to complete() after the commands above, because the browser may be loading the &#8220;myScript.js&#8221; file asynchronously, which means that our call may occur before the function has finished (or even started) loading.</p>
<p>Here we set up two different event handlers on the newly created script tag. Depending on the browser, one or the other of these two handlers is supposed to be called when the script has finished loading. The <span class="highlighted">onreadystatechange</span> handler works on <span class="highlighted">IE</span> only. The <span class="highlighted">onload</span> handler works on <span class="highlighted">Gecko browsers</span> and <span class="highlighted">Opera</span>.</p>
<p>The readyState theoretically goes through a series of states:</p>
<p><span class="highlighted">0</span> 	uninitialized<br /><span class="highlighted">1</span>	loading<br /><span class="highlighted">2</span>	loaded<br /><span class="highlighted">3</span>	interactive<br /><span class="highlighted">4</span>	complete</p>
<pre>
          			<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">HTTPUTILITY</span> <span class="keyword">=  {</span></code>
						<code class="text-indent-1"><span class="variable">getScript</span><span class="keyword">: function(</span><span class="string">url</span>, <span class="variable">callback</span><span class="keyword">) {</span></code>
							<code class="text-indent-2"><span class="keyword">var</span> <span class="variable">isLoaded</span> = <span class="value">false</span>;</code>
							<code class="text-indent-2"><span class="keyword">var</span> <span class="element">script</span> = <span class="element">document.</span><span class="keyword">createElement(</span><span class="string">"script"</span><span class="keyword">)</span>;</code>
							<code class="text-indent-2"><span class="element">script</span>.<span class="keyword">setAttribute(</span><span class="attribute">"language"</span>, <span class="value">"javascript"</span><span class="keyword">)</span>;</code>
							<code class="text-indent-2"><span class="element">script</span>.<span class="keyword">setAttribute(</span><span class="attribute">"type"</span>, <span class="value">"text/javascript"</span><span class="keyword">)</span>;</code>
							<code class="text-indent-2"><span class="element">script</span>.<span class="keyword">setAttribute(</span><span class="attribute">"src"</span>, <span class="value">url</span><span class="keyword">)</span>;</code>
							<code class="text-indent-2"><span class="element">script</span>.<span class="keyword">onload</span> = <span class="element">script.</span><span class="keyword">onreadystatechange</span> = <span class="keyword">function() {</span></code>
								<code class="text-indent-3"><span class="keyword">if (</span><span class="keyword">!</span><span class="variable">isLoaded</span> <span class="keyword">&#038;&#038;</span> (<span class="keyword">!</span><span class="keyword">this</span>.<span class="attribute">readyState</span> <span class="keyword">||</span> <span class="keyword">this</span>.<span class="attribute">readyState</span> <span class="keyword">==</span> <span class="string">"loaded"</span> <span class="keyword">||</span> <span class="keyword">this</span>.<span class="attribute">readyState</span> <span class="keyword">==</span> <span class="string">"complete"</span><span class="keyword">)) {</span></code>
									<code class="text-indent-4"><span class="variable">isLoaded</span> = <span class="value">true</span>;</code>
									<code class="text-indent-4"><span class="keyword">if (</span><span class="keyword">typeof</span> <span class="variable">callback</span> <span class="keyword">===</span> <span class="string">"function"</span><span class="keyword">)</span></code>
										<code class="text-indent-5"><span class="variable">callback()</span>;</code>
									<code class="text-indent-4"><span class="keyword">if (</span><span class="keyword">this.tagName.toLowerCase()</span> <span class="keyword">==</span> <span class="string">"script"</span><span class="keyword">)</span></code>
										<code class="text-indent-5"><span class="keyword">document.getElementsByTagName(</span><span class="string">"head"</span>)<span class="value">[0]</span>.<span class="keyword">removeChild(</span><span class="keyword">this</span><span class="keyword">)</span>;</code>
								<code class="text-indent-3"><span class="keyword">}</span></code>
							<code class="text-indent-2"><span class="keyword">}</span></code>
							<code class="text-indent-2"><span class="keyword">var</span> <span class="element">head</span> = <span class="element">document</span>.<span class="keyword">getElementsByTagName(</span><span class="string">"head"</span><span class="keyword">)</span><span class="value">[0]</span>;</code>
							<code class="text-indent-2"><span class="element">head</span>.<span class="keyword">appendChild(</span><span class="element">script</span><span class="keyword">)</span>;</code>
						<code class="text-indent-1"><span class="keyword">}</span></code>
          			<code class="text-indent-0"><span class="keyword">};</span></code>
          		</pre>
<pre>
          			<code class="text-indent-0">function complete() {</code>
          				<code class="text-indent-1">alert("complete");</code>
          			<code class="text-indent-0">}</code>
          			<code class="text-indent-0"><span class="keyword">HTTPUTILITY.getScript(</span><span class="string">"myScript.js"</span>, <span class="keyword">function(){</span><span class="variable">complete()</span>;<span class="keyword">})</span>;</code>
          		</pre>
<div class="button-row clearfix">
<p class="button-style preview-icon"><a href="http://www.lingihuang.com/viviancrap/wp-content/uploads/dynamically_load_script/index.html" target="_blank">View<span>Demo</span></a></p>
</p></div>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">document.write</div>
</p></div>
</p></div>
</p></div>
</p></div>
<pre>
          			<code class="text-indent-0"><span class="element">document</span>.<span class="keyword">write(</span><span class="string">'<' +</span> <span class="string">'script type=</span><span class="value">"text/javascript"</span> <span class="string">src=</span><span class="value">"http://www.lingihuang.com/libraries/library.js"</span><span class="string">' +</span> <span class="string">'><' +</span> <span class="string">'/script>'</span><span class="keyword">)</span>;</code>
          		</pre>
<div class="button-row clearfix">
<p class="button-style preview-icon"><a href="http://www.lingihuang.com/viviancrap/wp-content/uploads/dynamically_load_script/document.write.html" target="_blank">View<span>Demo</span></a></p>
</p></div>
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Related Posts</div>
</p></div>
</p></div>
</p></div>
</p></div>
<ul class="entry-list">
<li><a href="http://unixpapa.com/js/dyna.html" target="_blank">Dynamic Script Loading</a></li>
<li><a href="http://www.nczonline.net/blog/2009/07/28/the-best-way-to-load-external-javascript/" target="_blank">The Best Way to Load External Javascript</a></li>
<li><a href="http://www.phpied.com/javascript-include-ready-onload/" target="_blank">Javascript Inclusion</a></li>
<li><a href="http://ntt.cc/2008/02/10/4-ways-to-dynamically-load-external-javascriptwith-source.html" target="_blank">4 Ways To Dynamically Load External Javascript</a></li>
</ul></div>
]]></content:encoded>
			<wfw:commentRss>http://www.lingihuang.com/viviancrap/archives/dynamically-load-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Disable Text Selection</title>
		<link>http://www.lingihuang.com/viviancrap/archives/disable-text-selection/</link>
		<comments>http://www.lingihuang.com/viviancrap/archives/disable-text-selection/#comments</comments>
		<pubDate>Wed, 15 Apr 2009 15:10:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.lingihuang.com/viviancrap/?p=43</guid>
		<description><![CDATA[




Disable Text Selection





With CSS

          				&#60;div unselectable="on"&#62;&#60;/div&#62; /* For IE */
          			

          				.classname {
          					user-select: none;
     [...]]]></description>
			<content:encoded><![CDATA[<div class="entry-content">
<div class="heading-row">
<div class="left-col">
<div class="right-col">
<div class="body-col">
<div class="title">Disable Text Selection</div>
</p></div>
</p></div>
</p></div>
</p></div>
<ul class="entry-list">
<li>With CSS</li>
<pre>
          				<code class="text-indent-0"><span class="element">&lt;div</span> <span class="attribute">unselectable</span>=<span class="value">"on"</span><span class="element">&gt;</span><span class="element">&lt;/div&gt;</span> <span class="comment">/* For IE */</span></code>
          			</pre>
<pre>
          				<code class="text-indent-0"><span class="class">.classname</span> <span class="keyword">{</span></code>
          					<code class="text-indent-1"><span class="attribute">user-select</span>: <span class="value">none</span>;</code>
          					<code class="text-indent-1"><span class="attribute">-moz-user-select</span>: <span class="value">none</span>; <span class="comment">/* For Firefox */</span></code>
          					<code class="text-indent-1"><span class="attribute">-khtml-user-select</span>: <span class="value">none</span>; <span class="comment">/* For Safari */</span></code>
          					<code class="text-indent-1"><span class="attribute">-o-user-select</span>: <span class="value">none</span>; <span class="comment">/* For Opera */</span></code>
          				<code class="text-indent-0"><span class="keyword">}</span></code>
          			</pre>
<li>With Javascript</li>
<pre>
          				<code class="text-indent-0"><span class="keyword">var</span> <span class="variable">disableTextSelection</span> <span class="keyword">= function(</span><span class="variable">element</span><span class="keyword">) {</span></code>
          					<code class="text-indent-1"><span class="element">element</span>.<span class="attribute">unselectable</span> = <span class="value">"on"</span>; <span class="comment">/* For IE */</span></code>
          					<code class="text-indent-1"><span class="element">element</span>.<span class="keyword">style</span>.<span class="attribute">UserSelect</span> = <span class="value">"none"</span>;</code>
          					<code class="text-indent-1"><span class="element">element</span>.<span class="keyword">style</span>.<span class="attribute">MozUserSelect</span> = <span class="value">"none"</span>; <span class="comment">/* For Firefox */</span></code>
          					<code class="text-indent-1"><span class="element">element</span>.<span class="keyword">style</span>.<span class="attribute">KhtmlUserSelect</span> = <span class="value">"none"</span>; <span class="comment">/* For Safari */</span></code>
          					<code class="text-indent-1"><span class="element">element</span>.<span class="keyword">style</span>.<span class="attribute">OUserSelect</span> = <span class="value">"none"</span>; <span class="comment">/* For Opera */</span></code>
          				<code class="text-indent-0"><span class="keyword">};</span></code>
          			</pre>
</ul></div>
]]></content:encoded>
			<wfw:commentRss>http://www.lingihuang.com/viviancrap/archives/disable-text-selection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
