<?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; html</title>
	<atom:link href="http://siphon9.net/loune/tag/html/feed/" rel="self" type="application/rss+xml" />
	<link>http://siphon9.net/loune</link>
	<description></description>
	<lastBuildDate>Wed, 18 May 2011 12:10:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<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; title: ; notranslate">&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>

