User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 397,851 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,322 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 17746 | Replies: 20 | Solved
Reply
Join Date: Jun 2007
Posts: 4
Reputation: deepthianns is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
deepthianns's Avatar
deepthianns deepthianns is offline Offline
Newbie Poster

Detect browser window closing event in Firefox -

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,806
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 338
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Detect browser window closing event in Firefox -

  #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."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Jun 2007
Posts: 4
Reputation: deepthianns is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
deepthianns's Avatar
deepthianns deepthianns is offline Offline
Newbie Poster

Re: Detect browser window closing event in Firefox -

  #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  
Join Date: Jun 2006
Location: India
Posts: 6,806
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 338
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Detect browser window closing event in Firefox -

  #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."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Jun 2007
Posts: 4
Reputation: deepthianns is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
deepthianns's Avatar
deepthianns deepthianns is offline Offline
Newbie Poster

Re: Detect browser window closing event in Firefox -

  #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  
Join Date: Jun 2006
Location: India
Posts: 6,806
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 338
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Detect browser window closing event in Firefox -

  #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."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Jan 2007
Posts: 2,524
Reputation: MidiMagic is on a distinguished road 
Rep Power: 7
Solved Threads: 105
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Posting Maven

Re: Detect browser window closing event in Firefox -

  #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  
Join Date: Jun 2006
Location: India
Posts: 6,806
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 338
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Detect browser window closing event in Firefox -

  #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."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Jun 2007
Posts: 4
Reputation: deepthianns is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
deepthianns's Avatar
deepthianns deepthianns is offline Offline
Newbie Poster

Re: Detect browser window closing event in Firefox

  #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  
Join Date: Jun 2006
Location: India
Posts: 6,806
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 338
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Detect browser window closing event in Firefox -

  #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."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 8:14 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC