Solved

How to Distinguish between Refresh Button/F5 and Close Page X Button?

Posted on 2006-06-26
6
1,125 Views
Last Modified: 2008-01-09
Hi All,
I have a page that on its onunload event i show an alert for the user.
This alert must be shown only when the user click the x button of the page to close it.
I notcied that when the page is refreshed  by the browser refresh button or pressing the F5 key...the alert is shown too...the reason that the onunload event is called here too.
Is there any way to know if the Refresh Button or F5 is the pressed and not the close x button?
Any comment will be very helpfull
Thanks in advanced.
samjav
0
Comment
Question by:samjav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 15

Expert Comment

by:justinbillig
ID: 16983358
No, there is no way to determine if the user is closing the browser or refreshing the page. That is just the inherit behavior of the browser.
0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 16984067
What u need to do is capture the window.close() event other then ONUNLOAD

You can add two lines to your script:

    function window_close() { alert("I shall not close"); }
    window.close = window_close;

Now call to window.close() will show the new alert. You can store the
original function aside, e.g.

    var backup = new Object(); backup.close = window.close;

(naturally, you do that before you reassign window.close). Now you can
perform all your verification and call the orignal function to close the
window.


U can  capture the reload/refresh event in Javascript as below
You would use the onLoad event in the body tag.
<BODY onLoad="some_function()">

Try it..
0
 

Author Comment

by:samjav
ID: 16990144
hi kaliyugkaarjun
after i put the script codes you mentioned, where shall i call it?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Accepted Solution

by:
kaliyugkaarjun earned 40 total points
ID: 16991698
0
 

Expert Comment

by:mailtovenkats
ID: 21884308
Hi,
im also facing same problem.
Could any one help me on this...
How can I distinguish the browser close condition from the refresh condition and close button
0
 

Expert Comment

by:mailtovenkats
ID: 21884384
Hi
   im also facing same problem. How can I distinguish the browser close from the refresh in page unload.

i could not able to get solution for this.
basically , when user close the browser , i would like to call one function and when page refersh that function should not be called...

Please advice...i trying this for so many days.. but no proper result

Thanks in advance..
venki
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question