Detect browser window closing event in Firefox -

Please support our JavaScript / DHTML / AJAX advertiser: 50% Off 6 Months Web Hosting from 1&1. The World's #1 Host!
Thread Solved

Join Date: Jun 2007
Posts: 4
Reputation: deepthianns is an unknown quantity at this point 
Solved Threads: 0
deepthianns's Avatar
deepthianns deepthianns is offline Offline
Newbie Poster

Detect browser window closing event in Firefox -

 
0
  #1
Jul 2nd, 2007
Hi,

I want to execute some java script code when I close the browser. The following code is executing in I.E. But I want to execute in Firefox and Netscape.

<html>
<head>
<script language="JavaScript">

function doUnload(evt)
{
var e = (window.event) ? window.event : evt;

if (e.clientX < 0 && e.clientY < 0){
{

alert("window closing....");

}
}

</script>

</head>
<body onunload="doUnload(event)">

</body>
</html>

In firefox clientX and clientY are getting as "undefined"....

Can any One Please help me.

Thanks in advance

Deepthi
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,767
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 493
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Detect browser window closing event in Firefox -

 
0
  #2
Jul 2nd, 2007
This a a cross browser compatible script for finding out the mouse coordinates using Javascript. Referred from this site.

  1. function doSomething(e) {
  2. var posx = 0;
  3. var posy = 0;
  4. if (!e) var e = window.event;
  5. if (e.pageX || e.pageY) {
  6. posx = e.pageX;
  7. posy = e.pageY;
  8. }
  9. else if (e.clientX || e.clientY) {
  10. posx = e.clientX + document.body.scrollLeft
  11. + document.documentElement.scrollLeft;
  12. posy = e.clientY + document.body.scrollTop
  13. + document.documentElement.scrollTop;
  14. }
  15. alert(posx + ", " + posy);
  16. }
Last edited by ~s.o.s~; Jul 2nd, 2007 at 4:04 am.
I don't accept change; I don't deserve to live.

Sacrifice is a painful, pure and beautiful thing.

Dammit, Jones, What the Hell Are Knoll Pointers?!
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 4
Reputation: deepthianns is an unknown quantity at this point 
Solved Threads: 0
deepthianns's Avatar
deepthianns deepthianns is offline Offline
Newbie Poster

Re: Detect browser window closing event in Firefox -

 
0
  #3
Jul 3rd, 2007
Hi,

Thanks for your reply..

I am getting the mouse coordinates in IE, but not in firefox using the above codes.

My requirement is to execute some function while closing the browser window. It should not work while refreshing the page.

Thank you.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,767
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 493
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Detect browser window closing event in Firefox -

 
0
  #4
Jul 4th, 2007
The coordinates are working fine in my case. There must be some error on your part. Post the code if it still doesn't work.

As for executing a function when the window closes, you can use the onunload callback function.

Here is an example:
  1. <html>
  2. <head>
  3. <script>
  4. function doSomething(e) {
  5. var posx = 0;
  6. var posy = 0;
  7. if (!e) var e = window.event;
  8. if (e.pageX || e.pageY) {
  9. posx = e.pageX;
  10. posy = e.pageY;
  11. }
  12. else if (e.clientX || e.clientY) {
  13. posx = e.clientX + document.body.scrollLeft
  14. + document.documentElement.scrollLeft;
  15. posy = e.clientY + document.body.scrollTop
  16. + document.documentElement.scrollTop;
  17. }
  18. alert(posx + ", " + posy);
  19. }
  20.  
  21. function confirmMe()
  22. {
  23. alert("Thank you for visiting us!!");
  24. }
  25. </script>
  26. </head>
  27. <body onunload="confirmMe();">
  28. <div onclick="doSomething(event);">Hello there</div>
  29. </body>
  30. </html>
I don't accept change; I don't deserve to live.

Sacrifice is a painful, pure and beautiful thing.

Dammit, Jones, What the Hell Are Knoll Pointers?!
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 4
Reputation: deepthianns is an unknown quantity at this point 
Solved Threads: 0
deepthianns's Avatar
deepthianns deepthianns is offline Offline
Newbie Poster

Re: Detect browser window closing event in Firefox -

 
0
  #5
Jul 5th, 2007
Yes, the code is detecting the mouse coordinates. But it is working when REFRESHING the page and on every other button events(back button..) .

Is there any possible way to execute some functions, only on closing the browser window(in firefox)?

Thank you.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,767
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 493
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Detect browser window closing event in Firefox -

 
0
  #6
Jul 5th, 2007
> Is there any possible way to execute some functions, only on
> closing the browser window(in firefox)?
As far as I know, no.
I don't accept change; I don't deserve to live.

Sacrifice is a painful, pure and beautiful thing.

Dammit, Jones, What the Hell Are Knoll Pointers?!
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,266
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 170
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: Detect browser window closing event in Firefox -

 
-1
  #7
Jul 13th, 2007
Even if you could do that, Firefox closes so quickly that nobody could read it.

If somebody clicks the X, they want to close the program now. They don't want it to do any more.

If IE can do it, it is because IE has a nonstandard extension to web code. Never use nonstandard code.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,767
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 493
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Detect browser window closing event in Firefox -

 
0
  #8
Jul 14th, 2007
> Even if you could do that, Firefox closes so quickly that nobody could read it.
The 'onunload' event fires before the browser is closed and if an alert is fired, stops the window from closing so 'closes so quickly' is not an issue as such. The problem here is that the OP wants a function which would be fired _if and only if_ the browser is closed which I don't think is possible using any standard way.
I don't accept change; I don't deserve to live.

Sacrifice is a painful, pure and beautiful thing.

Dammit, Jones, What the Hell Are Knoll Pointers?!
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 4
Reputation: deepthianns is an unknown quantity at this point 
Solved Threads: 0
deepthianns's Avatar
deepthianns deepthianns is offline Offline
Newbie Poster

Re: Detect browser window closing event in Firefox

 
0
  #9
Jul 17th, 2007
My purpose is to delete a record from the database table while closing (only) the browser window using X.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,767
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 493
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Detect browser window closing event in Firefox -

 
0
  #10
Jul 18th, 2007
AFAIK, you can't do that since before the document is being unloaded, submitting the form is an illegal action.
I don't accept change; I don't deserve to live.

Sacrifice is a painful, pure and beautiful thing.

Dammit, Jones, What the Hell Are Knoll Pointers?!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the JavaScript / DHTML / AJAX Forum


Views: 51905 | Replies: 27
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC