Solved

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

Posted on 2006-06-26
6
1,118 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
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

785 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