views:

207

answers:

2

Hi all, I have written code like this to throw a pop up when my website is closed. But this throws the pop up even if i refresh my page. Please help me to throw popup only when i close the browser of that website. (disable popup on refreshing page)

<body onunload="javascript: exitpop()">

<script type="text/javascript">
function exitpop()
{
my_window= window.open ("","mywindow1","status=1,width=600,height=400"); 
my_window.document.write('<h1>My Team</h1><p>Thank you</p><p>If you accidentally closed website click <a href="http://www.google.com"&gt;here&lt;/a&gt; to go back to our website</p>'); 
}.
A: 

onUnLoad is called on a refresh because the browser is requesting a new page (well, the same page in the case of a refresh, but it is still a new call).

I am not sure if there is a way to check where the user is going next, but if there is you could compare it to their current url, if they are the same then the user is just refreshing and the script doesn't need to be run.

Good luck

Barlow Tucker
yes... this sounds interesting... can you help me on how to compare current URL and next URL.. i mean the if-else part
A: 

Sadly, this is not possible. To the browser, it is essentially the same action.

However, if you just use a confirm() box, you can catch a user that is trying to leave without linking them back. If they are refreshing or if they really want to leave, hitting "OK" will allow that action to occur.

Let me know if you need this explained further.

Khan
Hi Khan! Yes. I am eager to know how that works. please help me out with your code explanation.
<script type="text/javascript"> function exitFunc() { return "Are you sure you want to do that?"; }</script><body onbeforeunload="return exitFunc();"></body>
Khan