<?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>Phine Solutions &#187; javascript</title>
	<atom:link href="http://www.phinesolutions.com/topics/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://www.phinesolutions.com</link>
	<description>A web log for web work</description>
	<lastBuildDate>Wed, 30 Jun 2010 16:23:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Trying out Google Web Toolkit</title>
		<link>http://www.phinesolutions.com/trying-out-google-web-toolkit.html</link>
		<comments>http://www.phinesolutions.com/trying-out-google-web-toolkit.html#comments</comments>
		<pubDate>Tue, 09 Jun 2009 02:08:50 +0000</pubDate>
		<dc:creator>1.618</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.phinesolutions.com/?p=238</guid>
		<description><![CDATA[After watching some Google IO keynote videos I got interested in Google Web Toolkit and played with it a little bit. It is quite an interesting tool. Basically the tool helps Java developers to develop JavaScript using the familiar Java programming language on familiar development IDE such as Eclipse. And probably more importantly, it takes [...]]]></description>
			<content:encoded><![CDATA[<p>After watching some Google IO keynote videos I got interested in Google Web Toolkit and played with it a little bit. It is quite an interesting tool. Basically the tool helps Java developers to develop JavaScript using the familiar Java programming language on familiar development IDE such as Eclipse. And probably more importantly, it takes care of the nittygritty details such as browser compatibility and code optimization. So one can write it once and be pretty comfortable about running the code on different type of browsers.</p>
<p>Unlike other Java to JavaScript frameworks (jMaki for example) that require actually to be hosted in a Java VM container, GWT produces the client side JavaScript code as a final product. So Java, in this case, is just a tool, not part of the end result. Of course, if there is server in the backend to handle the RPC calls,  Java Servlets can be used to facilitate that, but it is not mandatory.</p>
<p>GWT is not just a tool for Java developers who don&#8217;t like coding JavaScript. It also put AJAX into better perspective by separating the client and service code in the code base. Using JSON as the messaging protocol between the client and server, a GWT client application can talk to any web server that speaks the language, which doesn&#8217;t have to be implemented in Java. GWT also provides a way to work around SOP &#8211; the Same Origin Policy.</p>
<p>SOP is the security policy that is in the web browser to stop a client script to communicate with web server that the script is not originally from, in another word, any arbitrary web server. This is a necessary security measurement, but it makes the client less distributive. For example, a web service provider would like to distribute a small version of client script, which can be embedded in any web sites, and get the dynamic information from the service host. GWT handles this by requiring server to return JSON output in a &#8220;&lt;script&gt;&#8221; tag, and a callback function will also need to be defined to handle the output.</p>
<p>With the advance of web technology, JavaScript will play much bigger part in rich web applications in the future. I think the biggest strength of GWT is its capability to scale up the size of a JavaScript project. Following the Java&#8217;s footprint a project created using GWT can be better managed, tested and collaborated among developers.</p>
<p>One drawback is that one has to be familiar with Java. And obviously there is a little bit of learning curve on those GWT GUI APIs. Nonetheless it is a great tool for Java developer to natually adopt and use to create some great JavaScript applications.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phinesolutions.com/trying-out-google-web-toolkit.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why you should use typeof operator to check whether a variable is defined in Javascript</title>
		<link>http://www.phinesolutions.com/why-you-should-use-typeof-operator-to-check-whether-a-variable-is-defined-in-javascript.html</link>
		<comments>http://www.phinesolutions.com/why-you-should-use-typeof-operator-to-check-whether-a-variable-is-defined-in-javascript.html#comments</comments>
		<pubDate>Fri, 19 Sep 2008 18:12:34 +0000</pubDate>
		<dc:creator>1.618</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.phinesolutions.com/?p=162</guid>
		<description><![CDATA[We often check if a variable is defined or not in javascript before proceeding the next line of code. Usually the code somewhat looks like this:
var myUndefinedVar = 1;
if (myUndefinedVar) {
  // do some stuff
}
Even the browser will throw out an Javascript error, the code works since there is no else statement or other [...]]]></description>
			<content:encoded><![CDATA[<p>We often check if a variable is defined or not in javascript before proceeding the next line of code. Usually the code somewhat looks like this:</p>
<blockquote><p><span style="text-decoration: line-through;">var myUndefinedVar = 1;</span></p>
<p>if (myUndefinedVar) {</p>
<p>  // do some stuff</p>
<p>}</p></blockquote>
<p>Even the browser will throw out an Javascript error, the code works since there is no else statement or other intended logics beneath the if block in this case. Otherwise, we won&#8217;t be so lucky since the browser will stop executing the rest of the statements. A better way is using the typeof operator, which will not result in an browser error even the variable is undefined. Here is a code example:</p>
<blockquote><p>if (typeof(myUndefinedVar) == &#8216;undefined&#8217;) {</p>
<p>  // do this</p>
<p>} else {</p>
<p>  // do that</p>
<p>}</p></blockquote>
<p>Notice that, the following code will work whether the document element is there or not.</p>
<blockquote><p>if (document.getElementById(&#8217;blah&#8217;)) {</p>
<p>}</p></blockquote>
<p>The reason is that document is always a valid object and getElementById will either return an object or a null value, which in either case, will not cause the browser to throw up.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phinesolutions.com/why-you-should-use-typeof-operator-to-check-whether-a-variable-is-defined-in-javascript.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use JQuery to adjust the iframe height</title>
		<link>http://www.phinesolutions.com/use-jquery-to-adjust-the-iframe-height.html</link>
		<comments>http://www.phinesolutions.com/use-jquery-to-adjust-the-iframe-height.html#comments</comments>
		<pubDate>Sun, 08 Jun 2008 19:21:51 +0000</pubDate>
		<dc:creator>1.618</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.phinesolutions.com/?p=145</guid>
		<description><![CDATA[Although frame is generally not recommended on most of the web pages, iFrame can still be useful in some occasions, especially as an Ajax alternative. One strength of Ajax is that it greatly reduces (or at least it appears so) the page response time by only changing a small part of the page. Using Ajax [...]]]></description>
			<content:encoded><![CDATA[<p>Although frame is generally not recommended on most of the web pages, iFrame can still be useful in some occasions, especially as an Ajax alternative. One strength of Ajax is that it greatly reduces (or at least it appears so) the page response time by only changing a small part of the page. Using Ajax to submit a form is a great example. This usability improvement doesn&#8217;t come free though. In order to use Ajax to send the form data back to the server, the JavaScript code has to be written to collect the form data and append them to the request URL as GET or POST parameters. Frameworks like JQuery or Prototype make this process a lot easier.</p>
<p>However there is some limitation to use Ajax to post a multi-part form. Although <a href="http://www.captain.at/ajax-file-upload.php">it might be possible</a>, it is definitely not a clean implementation, and it is not supported by most of the browsers. In this case, iFrame may be a good alternative. The inner frame page will handle the multi-part form and the parent page will have the similar Ajax effect. That being said, iFrame&#8217;s biggest problem is that its width and height are set right from the start and it won&#8217;t adjust based on the source content. This will leave the ugly scrolling bars around the iFrame, or some content will be hidden if the scroll bar is disabled. For most of the pages, the width is somewhat less of a problem but the  height is harder to control and set correctly.</p>
<p>The good news is there are ways to dynamically adjust the height of the iFrame based on the inner content. There are different ways to achieve this using JavaScript. I found this approach is the best:</p>
<p>Using jQuery, we can add the following code in the iframe source content:</p>
<blockquote><p>&lt;script type=&#8221;text/javascript&#8221;&gt;<br />
$(document).ready(function() {<br />
var theFrame = $(&#8221;#iFrameToAdjust&#8221;, parent.document.body);<br />
theFrame.height($(document.body).height() + 30);<br />
});<br />
&lt;/script&gt;</p></blockquote>
<p>The JavaScript will get the iframe object from the parent DOM and change its height according to the size of the current document after the document is loaded (very important to get the true size). I like it because it is less intrusive to the page where the iframe is on, and the iframe source kind of &#8220;take care of its own size&#8221; when being displayed. If the source page is displayed as regular page, there simply will not be any adjustment. With the help of jQuery the code is quite clean and simple. Of course the iframe source has to be an internal page and the developer has the permission to add the code.</p>
<p>From my testing result, this works in FF2, IE7 and Opera 9.25, not in Safari 3.1 for Windows though.</p>
<p>Some updates:</p>
<p>I found it work better to put the JavaScript which adjusts the frame height in the body onLoad attribute. Basically &#8220;ready&#8221; function will be kicked off when the DOM is loaded, at which point the page may or may not be completely loaded. The onLoad event will be a better bet in this case since we need the actual size of the page including all the images.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phinesolutions.com/use-jquery-to-adjust-the-iframe-height.html/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>remind users when they leave the page</title>
		<link>http://www.phinesolutions.com/remind-users-when-they-leave-the-page.html</link>
		<comments>http://www.phinesolutions.com/remind-users-when-they-leave-the-page.html#comments</comments>
		<pubDate>Fri, 05 Jan 2007 16:01:23 +0000</pubDate>
		<dc:creator>1.618</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[usability design]]></category>

		<guid isPermaLink="false">http://www.phinesolutions.com/remind-users-when-they-leave-the-page.html</guid>
		<description><![CDATA[When a use visits a web page and enter some information into a form, he may intentionally or unintentionally leave the page without clicking on the submit button. It is a good usability effort to remind the user of the possible changes that have happened.
We can use a javascript to implement this.
/** checkChange.js **/&#160;
var alertLeavingPage [...]]]></description>
			<content:encoded><![CDATA[<p>When a use visits a web page and enter some information into a form, he may intentionally or unintentionally leave the page without clicking on the submit button. It is a good usability effort to remind the user of the possible changes that have happened.</p>
<p>We can use a javascript to implement this.</p>
<blockquote><p>/** checkChange.js **/&nbsp;</p>
<p>var alertLeavingPage = false; /* flag to turn on the alert */<br />var pageInputChanged = false; /* the flag of changes made */</p>
<p>// The onbeforeonload is called before the page is changed. It is not a standard<br />// method. But it is supported by IE and FF.<br />window.onbeforeunload = function(theEvent) {<br />&nbsp;&nbsp; if (pageInputChanged == true &amp;&amp; alertLeavingPage == true) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (typeof theEvent == &#39;undefined&#39;) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; theEvent = window.event;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (theEvent) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; theEvent.returnValue = &quot;You have made some changes on this page, please click OK to discard the changes or Cancel to stay on the page.&quot;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp; } else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br />&nbsp;&nbsp; }<br />}</p>
<p>/*<br />&nbsp;* The method should be called on the page where the change needs to be tracked.<br />&nbsp;* The form ID needs to be provided so the change event can be registered on certain form elements.<br />&nbsp;*/<br />function trackChange(formId) {<br />&nbsp;&nbsp; var formElem = document.getElementById(formId);<br />&nbsp;&nbsp; if (formElem) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (var i=0; i&lt;formElem.elements.length; i++) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var inputElem = formElem.elements[i];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (inputElem.type) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var inputType = inputElem.type.toLowerCase();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (inputType == &#39;text&#39; || inputType == &#39;radio&#39; || <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inputType == &#39;checkbox&#39; || inputType == &#39;select-one&#39; || inputType == &#39;textarea&#39;) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inputElem.onchange = setChangedFlag;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // turn on the alert flag<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alertLeavingPage = true;<br />&nbsp;&nbsp; }<br />}</p>
<p>/*<br />&nbsp;* The method which is tied to a change event.<br />&nbsp;*/<br />function setChangedFlag(evt) {<br />&nbsp;&nbsp; pageInputChanged = true;<br />} </p>
</blockquote>
<p>Of course you will need to include this script to the web page. Besides that, you also need to call the trackChange() method to register the form elements that we want to track. So a form Id needs to be set in order to register. A sample html page look like following:&nbsp;</p>
<blockquote><p>&lt;html&gt;</p>
<p>&lt;form id=&quot;the_form_id&quot; action=&quot;&quot; onsubmit=&quot;alertLeavingPage=false; /* no reminder for submission */&quot;&gt;&nbsp;</p>
<p>&lt;/form&gt;&nbsp;</p>
<p>&lt;script type=&quot;text/javascript&quot;&gt;<br />&lt;!&#8211; //<br />trackChange(&#39;the_form_id&#39;);</p>
<p>// &#8211;&gt;&lt;/script&gt;</p>
<p>&lt;/html&gt;&nbsp;</p>
</blockquote>
<p>Once they are added into the page, the user should see this prompt when they leave the page with un-submitted inputs.&nbsp;</p>
<p>&nbsp;<a href="http://www.phinesolutions.com/remind-users-when-they-leave-the-page.html/leaving-page-promptgif/" title="leaving-page-prompt.gif"><img src="http://www.phinesolutions.com/wp-content/uploads/leaving-page-prompt.gif" border="0" alt="leaving-page-prompt.gif" /></a></p>
<p>The method only detects whether there has been any change activity and it won&#39;t be able to tell whether there is change being made. For example, if someone checks a checkbox and unchecks it the script will think there has been change made. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.phinesolutions.com/remind-users-when-they-leave-the-page.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
