<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Javascript &#8220;close hook&#8221; for browser window</title>
	<atom:link href="http://amazing-development.com/archives/2008/11/25/javascript-close-hook-for-browser-window/feed/" rel="self" type="application/rss+xml" />
	<link>http://amazing-development.com/archives/2008/11/25/javascript-close-hook-for-browser-window/</link>
	<description>ruby, java and the rest</description>
	<lastBuildDate>Sun, 25 Jul 2010 00:49:12 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: schlumpf</title>
		<link>http://amazing-development.com/archives/2008/11/25/javascript-close-hook-for-browser-window/comment-page-1/#comment-124572</link>
		<dc:creator>schlumpf</dc:creator>
		<pubDate>Mon, 16 Nov 2009 16:34:11 +0000</pubDate>
		<guid isPermaLink="false">http://amazing-development.com/?p=333#comment-124572</guid>
		<description>Hi Ayusman, 
sorry for the delay in replying to your last comment. I copied your code into a html file and opened it in my browser. It works as expected. 

I added some code: 
&lt;tt&gt;onclick=&quot;needToConfirm=false&quot;&lt;/tt&gt; to the google link, and then it deactivates the popup message as expected. 

If you right-click the link and say &quot;open in new window&quot;, it does set the needToConfirm to false, too (similar effect when you add &lt;tt&gt;target=&quot;blank&quot;&lt;/tt&gt; to the link which opens the link in a new window) , but the main window is NOT relaoded and the needToConfirm remains false, and when you then refresh or close the main window, the warning message is not shown. That&#039;s a problem indeed, for which I have no solution. Perhaps there&#039;s some javascript action which can be executed AFTER a link was clicked, then we could use that to set needToConfirm=&quot;true&quot; again?</description>
		<content:encoded><![CDATA[<p>Hi Ayusman,<br />
sorry for the delay in replying to your last comment. I copied your code into a html file and opened it in my browser. It works as expected. </p>
<p>I added some code:<br />
<tt>onclick="needToConfirm=false"</tt> to the google link, and then it deactivates the popup message as expected. </p>
<p>If you right-click the link and say &#8220;open in new window&#8221;, it does set the needToConfirm to false, too (similar effect when you add <tt>target="blank"</tt> to the link which opens the link in a new window) , but the main window is NOT relaoded and the needToConfirm remains false, and when you then refresh or close the main window, the warning message is not shown. That&#8217;s a problem indeed, for which I have no solution. Perhaps there&#8217;s some javascript action which can be executed AFTER a link was clicked, then we could use that to set needToConfirm=&#8221;true&#8221; again?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: schlumpf</title>
		<link>http://amazing-development.com/archives/2008/11/25/javascript-close-hook-for-browser-window/comment-page-1/#comment-124571</link>
		<dc:creator>schlumpf</dc:creator>
		<pubDate>Mon, 16 Nov 2009 16:24:09 +0000</pubDate>
		<guid isPermaLink="false">http://amazing-development.com/?p=333#comment-124571</guid>
		<description>Hi Kamatchi, 
thanks for your comment. As far as I know, it&#039;s not possible to distinguish between refresh and close events. But I&#039;m not really an expert on this... I just copied the code from somewhere else.</description>
		<content:encoded><![CDATA[<p>Hi Kamatchi,<br />
thanks for your comment. As far as I know, it&#8217;s not possible to distinguish between refresh and close events. But I&#8217;m not really an expert on this&#8230; I just copied the code from somewhere else.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kamatchi</title>
		<link>http://amazing-development.com/archives/2008/11/25/javascript-close-hook-for-browser-window/comment-page-1/#comment-124553</link>
		<dc:creator>Kamatchi</dc:creator>
		<pubDate>Thu, 12 Nov 2009 07:06:33 +0000</pubDate>
		<guid isPermaLink="false">http://amazing-development.com/?p=333#comment-124553</guid>
		<description>hello

The above script is working fine while closing browser, but if i press refresh button it alerts the pop up message. I need the pop up only at the time of closing the browser not refresh or navigation through the pages.

Thanks in advance.</description>
		<content:encoded><![CDATA[<p>hello</p>
<p>The above script is working fine while closing browser, but if i press refresh button it alerts the pop up message. I need the pop up only at the time of closing the browser not refresh or navigation through the pages.</p>
<p>Thanks in advance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ayusman Mohanty</title>
		<link>http://amazing-development.com/archives/2008/11/25/javascript-close-hook-for-browser-window/comment-page-1/#comment-124530</link>
		<dc:creator>Ayusman Mohanty</dc:creator>
		<pubDate>Wed, 04 Nov 2009 04:44:55 +0000</pubDate>
		<guid isPermaLink="false">http://amazing-development.com/?p=333#comment-124530</guid>
		<description>&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
  &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
  &lt;head&gt;
  &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot; /&gt;
  &lt;title&gt;Untitled Document&lt;/title&gt;
  &lt;script type=&quot;text/javascript&quot;&gt;
  var needToConfirm = true;
  
  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
  if (needToConfirm)
  return &quot;You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?&quot;;
  }
  &lt;/script&gt; 
&lt;/head&gt;
  &lt;body&gt;
&lt;a href=&quot;http://www.google.com&quot;&gt;Test&lt;/a&gt;
  &lt;/body&gt;
  &lt;/html&gt;</description>
		<content:encoded><![CDATA[<p>&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;<br />
  &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;<br />
  &lt;head&gt;<br />
  &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot; /&gt;<br />
  &lt;title&gt;Untitled Document&lt;/title&gt;<br />
  &lt;script type=&quot;text/javascript&quot;&gt;<br />
  var needToConfirm = true;</p>
<p>  window.onbeforeunload = confirmExit;<br />
  function confirmExit()<br />
  {<br />
  if (needToConfirm)<br />
  return &quot;You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?&quot;;<br />
  }<br />
  &lt;/script&gt;<br />
&lt;/head&gt;<br />
  &lt;body&gt;<br />
&lt;a href=&quot;http://www.google.com&quot;&gt;Test&lt;/a&gt;<br />
  &lt;/body&gt;<br />
  &lt;/html&gt;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: schlumpf</title>
		<link>http://amazing-development.com/archives/2008/11/25/javascript-close-hook-for-browser-window/comment-page-1/#comment-124526</link>
		<dc:creator>schlumpf</dc:creator>
		<pubDate>Tue, 03 Nov 2009 14:28:35 +0000</pubDate>
		<guid isPermaLink="false">http://amazing-development.com/?p=333#comment-124526</guid>
		<description>Hi Ayusman,
you can&#039;t catch only the unexpected closing events (like refresh, close browser) -- since they&#039;re unexpected you don&#039;t know what they will be before they appear. Therefore you need to catch ALL closing events and explicitly exclude those events you know about (i.e. your own links and buttons) by placing the . If you&#039;re using JSF to create your website, it should be easy to add some code that makes the onclick appear automatically at each link (use a template or something).

Concerning your first question, can you post a whole html page with which I can reproduce the problem? You&#039;ll need to html-escape all &lt; and &gt; signs like this: &lt; and &gt; else it will be scrambled by the wordpress software.</description>
		<content:encoded><![CDATA[<p>Hi Ayusman,<br />
you can&#8217;t catch only the unexpected closing events (like refresh, close browser) &#8212; since they&#8217;re unexpected you don&#8217;t know what they will be before they appear. Therefore you need to catch ALL closing events and explicitly exclude those events you know about (i.e. your own links and buttons) by placing the . If you&#8217;re using JSF to create your website, it should be easy to add some code that makes the onclick appear automatically at each link (use a template or something).</p>
<p>Concerning your first question, can you post a whole html page with which I can reproduce the problem? You&#8217;ll need to html-escape all &lt; and &gt; signs like this: &amp;lt; and &amp;gt; else it will be scrambled by the wordpress software.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ayusman Mohanty</title>
		<link>http://amazing-development.com/archives/2008/11/25/javascript-close-hook-for-browser-window/comment-page-1/#comment-124525</link>
		<dc:creator>Ayusman Mohanty</dc:creator>
		<pubDate>Tue, 03 Nov 2009 12:23:13 +0000</pubDate>
		<guid isPermaLink="false">http://amazing-development.com/?p=333#comment-124525</guid>
		<description>Okay, looks like you are confused...See for example I have a website which uses JSF and links are defined in h:commandlink tag. Now if I want to make 1000 links as onclick my warning message function as false then I would not prefer to go to each link and add onclick false so in this case what will be the common fix?</description>
		<content:encoded><![CDATA[<p>Okay, looks like you are confused&#8230;See for example I have a website which uses JSF and links are defined in h:commandlink tag. Now if I want to make 1000 links as onclick my warning message function as false then I would not prefer to go to each link and add onclick false so in this case what will be the common fix?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ayusman Mohanty</title>
		<link>http://amazing-development.com/archives/2008/11/25/javascript-close-hook-for-browser-window/comment-page-1/#comment-124524</link>
		<dc:creator>Ayusman Mohanty</dc:creator>
		<pubDate>Tue, 03 Nov 2009 12:02:45 +0000</pubDate>
		<guid isPermaLink="false">http://amazing-development.com/?p=333#comment-124524</guid>
		<description>Did you get to understand the scenario which I had been trying to explain you?</description>
		<content:encoded><![CDATA[<p>Did you get to understand the scenario which I had been trying to explain you?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ayusman Mohanty</title>
		<link>http://amazing-development.com/archives/2008/11/25/javascript-close-hook-for-browser-window/comment-page-1/#comment-124523</link>
		<dc:creator>Ayusman Mohanty</dc:creator>
		<pubDate>Tue, 03 Nov 2009 11:53:42 +0000</pubDate>
		<guid isPermaLink="false">http://amazing-development.com/?p=333#comment-124523</guid>
		<description>Hey,

Thanks for your reply on this. What I wanted to tell you is: in an anchor tag to avoid the onClick function triggering the prompt box you are setting the onClick as false, right. So what&#039;s happening once you click this link and then when you try refreshing or closing the browser the Warning Message will not appear because the onClick is set as false... &lt;a href=&quot;http://www.google.com&quot; rel=&quot;nofollow&quot;&gt; 

Hope you got me now else I will again explain you...&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>Hey,</p>
<p>Thanks for your reply on this. What I wanted to tell you is: in an anchor tag to avoid the onClick function triggering the prompt box you are setting the onClick as false, right. So what&#8217;s happening once you click this link and then when you try refreshing or closing the browser the Warning Message will not appear because the onClick is set as false&#8230; <a href="http://www.google.com" rel="nofollow"> </p>
<p>Hope you got me now else I will again explain you&#8230;</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: schlumpf</title>
		<link>http://amazing-development.com/archives/2008/11/25/javascript-close-hook-for-browser-window/comment-page-1/#comment-124522</link>
		<dc:creator>schlumpf</dc:creator>
		<pubDate>Tue, 03 Nov 2009 11:15:24 +0000</pubDate>
		<guid isPermaLink="false">http://amazing-development.com/?p=333#comment-124522</guid>
		<description>Hi Ayusman, 
thanks for your comment! I&#039;m not sure if wordpress didn&#039;t scramble the test link in your comment - it was just empty &lt;a&gt;Test&lt;/a&gt;. If this is what you intended, then I think I know why the unhook-Thing doesn&#039;t work properly in your case. Normally, when you click a link, the browser reloads the page, which automatically resets all javascript variables. In your case, no reload is triggered and all variables remain unchanged - the needToConfirm variable will still be false.</description>
		<content:encoded><![CDATA[<p>Hi Ayusman,<br />
thanks for your comment! I&#8217;m not sure if wordpress didn&#8217;t scramble the test link in your comment &#8211; it was just empty &lt;a&gt;Test&lt;/a&gt;. If this is what you intended, then I think I know why the unhook-Thing doesn&#8217;t work properly in your case. Normally, when you click a link, the browser reloads the page, which automatically resets all javascript variables. In your case, no reload is triggered and all variables remain unchanged &#8211; the needToConfirm variable will still be false.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ayusman Mohanty</title>
		<link>http://amazing-development.com/archives/2008/11/25/javascript-close-hook-for-browser-window/comment-page-1/#comment-124521</link>
		<dc:creator>Ayusman Mohanty</dc:creator>
		<pubDate>Tue, 03 Nov 2009 10:20:52 +0000</pubDate>
		<guid isPermaLink="false">http://amazing-development.com/?p=333#comment-124521</guid>
		<description>Hello,

I appreciate the way you have opted for displaying warning message. But I am not satisfied with this code as it does not function properly. Browser close message should only be called when someone closes or refreshes not by clicking links. Links where you have mentioned onClick=&quot;unhook()&quot; works fine but after clicking such a link if you close or refresh the browser the Warning message does not appear, have you really tested it? If not then check this scenario...I would really thank you if you can resolve this issue. Even I had written the same, here it is:

// warning message javascript
var needToConfirm = true;
  
  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
    if (needToConfirm)
      return &quot;&quot;;
  }


and then for links: &lt;a&gt;Test&lt;/a&gt;

But once you click the link the state remains in FALSE and that&#039;s why the Warning message does not show. Let me know if you could fix this? 

Cheers,
Ayush</description>
		<content:encoded><![CDATA[<p>Hello,</p>
<p>I appreciate the way you have opted for displaying warning message. But I am not satisfied with this code as it does not function properly. Browser close message should only be called when someone closes or refreshes not by clicking links. Links where you have mentioned onClick=&#8221;unhook()&#8221; works fine but after clicking such a link if you close or refresh the browser the Warning message does not appear, have you really tested it? If not then check this scenario&#8230;I would really thank you if you can resolve this issue. Even I had written the same, here it is:</p>
<p>// warning message javascript<br />
var needToConfirm = true;</p>
<p>  window.onbeforeunload = confirmExit;<br />
  function confirmExit()<br />
  {<br />
    if (needToConfirm)<br />
      return &#8220;&#8221;;<br />
  }</p>
<p>and then for links: <a>Test</a></p>
<p>But once you click the link the state remains in FALSE and that&#8217;s why the Warning message does not show. Let me know if you could fix this? </p>
<p>Cheers,<br />
Ayush</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: schlumpf</title>
		<link>http://amazing-development.com/archives/2008/11/25/javascript-close-hook-for-browser-window/comment-page-1/#comment-119974</link>
		<dc:creator>schlumpf</dc:creator>
		<pubDate>Thu, 02 Jul 2009 18:26:55 +0000</pubDate>
		<guid isPermaLink="false">http://amazing-development.com/?p=333#comment-119974</guid>
		<description>Hi VJ, 
thanks for your comment! Your code works fine in my browser (Firefox 3.0.8 on Gentoo Linux). What browser are you using? 

One thing you might want to try is use the event.returnValue as described in the addendum to the original post. Or you could install &lt;a href=&quot;https://addons.mozilla.org/en-US/firefox/addon/1843&quot; rel=&quot;nofollow&quot;&gt;FireBug&lt;/a&gt;, the javascript debugging plugin for Firefox, and try to find out what&#039;s going on (if you are using Firefox at all). Hope this helps!</description>
		<content:encoded><![CDATA[<p>Hi VJ,<br />
thanks for your comment! Your code works fine in my browser (Firefox 3.0.8 on Gentoo Linux). What browser are you using? </p>
<p>One thing you might want to try is use the event.returnValue as described in the addendum to the original post. Or you could install <a href="https://addons.mozilla.org/en-US/firefox/addon/1843" rel="nofollow">FireBug</a>, the javascript debugging plugin for Firefox, and try to find out what&#8217;s going on (if you are using Firefox at all). Hope this helps!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: VJ</title>
		<link>http://amazing-development.com/archives/2008/11/25/javascript-close-hook-for-browser-window/comment-page-1/#comment-119954</link>
		<dc:creator>VJ</dc:creator>
		<pubDate>Thu, 02 Jul 2009 11:58:20 +0000</pubDate>
		<guid isPermaLink="false">http://amazing-development.com/?p=333#comment-119954</guid>
		<description>Schlumpf

The last post is for you, I&#039;ve addressed it wrongly.

Any help on that is much appreciated.

Thanks, VJ</description>
		<content:encoded><![CDATA[<p>Schlumpf</p>
<p>The last post is for you, I&#8217;ve addressed it wrongly.</p>
<p>Any help on that is much appreciated.</p>
<p>Thanks, VJ</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: VJ</title>
		<link>http://amazing-development.com/archives/2008/11/25/javascript-close-hook-for-browser-window/comment-page-1/#comment-119953</link>
		<dc:creator>VJ</dc:creator>
		<pubDate>Thu, 02 Jul 2009 11:55:33 +0000</pubDate>
		<guid isPermaLink="false">http://amazing-development.com/?p=333#comment-119953</guid>
		<description>Suhail

I using similar code to display a message box when the user navigates away from the page without saving it.

But I get javascript error : &quot;unspecified error&quot; when cancel button is clicked in the message box. 

Below is my code,

var needToConfirm = true;
window.onbeforeunload = confirmExit;
function confirmExit()
{
       if(needToConfirm)
       return &#039;You have made changes to the fields. Without clicking the Save button, your changes will be lost.&#039;;

}

Thanks in advance, VJ.</description>
		<content:encoded><![CDATA[<p>Suhail</p>
<p>I using similar code to display a message box when the user navigates away from the page without saving it.</p>
<p>But I get javascript error : &#8220;unspecified error&#8221; when cancel button is clicked in the message box. </p>
<p>Below is my code,</p>
<p>var needToConfirm = true;<br />
window.onbeforeunload = confirmExit;<br />
function confirmExit()<br />
{<br />
       if(needToConfirm)<br />
       return &#8216;You have made changes to the fields. Without clicking the Save button, your changes will be lost.&#8217;;</p>
<p>}</p>
<p>Thanks in advance, VJ.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: schlumpf</title>
		<link>http://amazing-development.com/archives/2008/11/25/javascript-close-hook-for-browser-window/comment-page-1/#comment-112349</link>
		<dc:creator>schlumpf</dc:creator>
		<pubDate>Wed, 08 Apr 2009 04:11:06 +0000</pubDate>
		<guid isPermaLink="false">http://amazing-development.com/?p=333#comment-112349</guid>
		<description>Hi Suhail, 
thanks for your comment! It is not possible to remove or change the inbuilt message. See also &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx&quot; onClick=&quot;unhook()&quot; target=&quot;_blank&quot; rel=&quot;nofollow&quot;&gt;the documentation on msdn.microsoft.com&lt;/a&gt; (section &quot;Remarks&quot;).</description>
		<content:encoded><![CDATA[<p>Hi Suhail,<br />
thanks for your comment! It is not possible to remove or change the inbuilt message. See also <a href="http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx" onClick="unhook()" target="_blank" rel="nofollow">the documentation on msdn.microsoft.com</a> (section &#8220;Remarks&#8221;).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: suhail</title>
		<link>http://amazing-development.com/archives/2008/11/25/javascript-close-hook-for-browser-window/comment-page-1/#comment-112260</link>
		<dc:creator>suhail</dc:creator>
		<pubDate>Tue, 07 Apr 2009 11:37:56 +0000</pubDate>
		<guid isPermaLink="false">http://amazing-development.com/?p=333#comment-112260</guid>
		<description>Can we customize the message displayed on the popup box?
I just want to display my required message without the inbuilt ones (Are you sure you want to navigate away from this page? Press OK to continue or Cancel to stay on the current page).</description>
		<content:encoded><![CDATA[<p>Can we customize the message displayed on the popup box?<br />
I just want to display my required message without the inbuilt ones (Are you sure you want to navigate away from this page? Press OK to continue or Cancel to stay on the current page).</p>
]]></content:encoded>
	</item>
</channel>
</rss>
