<?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>Playing on the frontier &#187; javascript</title>
	<atom:link href="http://siphon9.net/loune/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://siphon9.net/loune</link>
	<description></description>
	<lastBuildDate>Thu, 15 Jul 2010 12:58:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Javascript snippet to convert raw UTF8 to unicode</title>
		<link>http://siphon9.net/loune/2009/10/javascript-snippet-to-convert-raw-utf8-to-unicode/</link>
		<comments>http://siphon9.net/loune/2009/10/javascript-snippet-to-convert-raw-utf8-to-unicode/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 08:29:13 +0000</pubDate>
		<dc:creator>Loune</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[unicode]]></category>
		<category><![CDATA[useless]]></category>
		<category><![CDATA[utf-8]]></category>

		<guid isPermaLink="false">http://siphon9.net/loune/?p=101</guid>
		<description><![CDATA[For the I-don&#8217;t-a-sane-use-for-this department comes this piece of code which takes a stream of raw UTF-8 bytes, decodes it and fromCharCode it, rendering it in a unicode supported browser. A possible use would be if the web page character set is not UTF-8 and you want to display UTF-8. To use it, just put it [...]]]></description>
			<content:encoded><![CDATA[<p>For the I-don&#8217;t-a-sane-use-for-this department comes this piece of code which takes a stream of raw UTF-8 bytes, decodes it and fromCharCode it, rendering it in a unicode supported browser. A possible use would be if the web page character set is not UTF-8 and you want to display UTF-8. To use it, just put it in a script tag and call utf8decode(myrawutf8string). But seriously, all web pages should be UTF-8 by default nowadays. Here it is, in case anyone wants it:</p>
<pre class="brush: jscript;">
function TryGetCharUTF8(c, intc, b, i, count)
		{
			/*
			 * 10000000 80
			 * 11000000 C0
			 * 11100000 E0
			 * 11110000 F0
			 * 11111000 F8
			 * 11111100 FC
			 *
			 * FEFF = 65279 = BOM
			 *
			 * string musicalbassclef = &quot;&quot; + (char)0xD834 + (char)0xDD1E; 119070 0x1D11E
			 */

			if ((b.charCodeAt(i) &amp; 0x80) == 0)
			{
				intc = b.charCodeAt(i);
			}
			else
			{
				if ((b.charCodeAt(i) &amp; 0xE0) == 0xC0)
				{
					//if (i+1 &gt;= count) return false;
					intc = ((b.charCodeAt(i) &amp; 0x1F) &lt;&lt; 6) | ((b.charCodeAt(i + 1) &amp; 0x3F));

					i += 1;
				}
				else if ((b.charCodeAt(i) &amp; 0xF0) == 0xE0)
				{
					// 3 bytes Covers the rest of the BMP
					//if (i+2 &gt;= count) return false;
					intc = ((b.charCodeAt(i) &amp; 0xF) &lt;&lt; 12) | ((b.charCodeAt(i + 1) &amp; 0x3F) &lt;&lt; 6) | ((b.charCodeAt(i + 2) &amp; 0x3F));
					alert(b.charCodeAt(i) + ' '+b.charCodeAt(i + 1) +' '+b.charCodeAt(i + 2));
					i += 2;
				}
				else if ((b.charCodeAt(i) &amp; 0xF8) == 0xF0)
				{
					intc = ((b.charCodeAt(i) &amp; 0x7) &lt;&lt; 18) | ((b.charCodeAt(i + 1) &amp; 0x3F) &lt;&lt; 12) | ((b.charCodeAt(i + 2) &amp; 0x3F) &lt;&lt; 6) | ((b.charCodeAt(i + 3) &amp; 0x3F));

					i += 1;
				}
				else
					return false;
			}
window.utf8_out_intc = intc;
window.utf8_out_i = i;
			return true;
		}

function utf8decode(s) {
	var ss = &quot;&quot;;
	for(utf8_out_i = 0; utf8_out_i &lt; s.length; utf8_out_i++) {
		TryGetCharUTF8(window.utf8_out_c, window.utf8_out_intc, s, window.utf8_out_i, s.length);
		ss += String.fromCharCode(window.utf8_out_intc);
	}
	return ss;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://siphon9.net/loune/2009/10/javascript-snippet-to-convert-raw-utf8-to-unicode/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Detecting the back (or refresh) button click</title>
		<link>http://siphon9.net/loune/2009/07/detecting-the-back-or-refresh-button-click/</link>
		<comments>http://siphon9.net/loune/2009/07/detecting-the-back-or-refresh-button-click/#comments</comments>
		<pubDate>Sat, 11 Jul 2009 09:21:35 +0000</pubDate>
		<dc:creator>Loune</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://siphon9.net/loune/?p=35</guid>
		<description><![CDATA[While developing a web app, I came across an interesting problem: I had a page which had a button to perform an action. If the button is clicked, the action request is sent to the server side script and redirected back to the same page but with a message displayed on the top of the [...]]]></description>
			<content:encoded><![CDATA[<p>While developing a web app, I came across an interesting problem: I had a page which had a button to perform an action. If the button is clicked, the action request is sent to the server side script and redirected back to the same page but with a message displayed on the top of the page (ie Your post has been submitted).</p>
<p>If you then navigate to another page but click back, you would see the same page with the same message popping up. I want to detect that we&#8217;re clicking back so we will hide the message. There are plenty of <a href="http://www.google.com.au/search?q=detect+back+button">solutions in google</a>, but a lot of them involved setting a cookie (what if cookies are disabled), or a server side script detecting referer (what if page is still cached?), or using time by detecting if the server page load time and the current time differs by a large amount (what if client time is wrong?). Without an ideal solution, I set about finding a new solution. Surely it can&#8217;t be hard to detect that we&#8217;ve already been in that same page. If only there was a way to save a flag just for that page and for the duration of the page session. I tried modifying the DOM, but that gets reverted when you click back. The onload event also get called again, so you can&#8217;t use that to differentiate.</p>
<p>I then remembered that at least on recent browsers, there exists a functionality in forms that retained form field information if you clicked back &#8211; very handy if you&#8217;re submitting a post and the connection died, you can just click back and your long winded post would be intact.</p>
<p><strong>Solution &#8211; Use a hidden form field to detect that we&#8217;ve been on this page before</strong></p>
<p>Building on this idea, it&#8217;s possible to temporarily store a flag on a hidden form field that says, yep I&#8217;ve been on this page before. Here is a code snippet:</p>
<pre class="brush: xml;">&lt;html&gt;
&lt;body&gt;
Try
&lt;a href=&quot;http://www.google.com/&quot;&gt;jumping to another page&lt;/a&gt;
&lt;/body&gt;

&lt;script&gt;

document.write(&quot;&lt;form style='display: none'&gt;&lt;input name='__detectback' id='__detectback' value=''&gt;&lt;/form&gt;&quot;);

function checkPageBackOrRefresh(load_id) {
if (document.getElementById('__detectback').value == load_id) {
return true;
} else {
document.getElementById('__detectback').value = load_id;
return false;
}
}

window.onload = function() {
if (checkPageBackOrRefresh('tt'))
alert('You clicked back or refreshed the page');
}

&lt;/script&gt;

&lt;/html&gt;</pre>
<p>Unfortunately, this solution does not work in some browsers where &#8220;fast back&#8221; (ie, fbcache in firefox) is enabled, as the fast back stores the scripting state so a onload does not trigger again.</p>
<p>The script should work fine with IE7 and IE8. With Firefox, it only works on certain pages. These pages seems to be pages that link to heavy javascripts (ie jquery?).</p>
<p>With fbcache enabled browsers, a possible solution would be to hide the message at the event onbeforeunload so it will not appear even when clicking back.</p>
]]></content:encoded>
			<wfw:commentRss>http://siphon9.net/loune/2009/07/detecting-the-back-or-refresh-button-click/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
