Solved

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

Posted on 2006-06-26
6
1,116 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now