Solved

jQuery on Window Close

Posted on 2012-03-14
6
433 Views
Last Modified: 2012-06-27
I need a jquery solution that will trigger a function on a windowClose event.

I currently have:

	<script type='text/javascript'>
	function closeIt(){
	     alert("Page has Closed");
		// logOutUser();
	  }
	$(window).unload(function() {
	  	closeIt();
	});
	</script>

Open in new window


The problem with .unload() is that it's trigger anytime the DOM is unloaded. So every time you click any link on the page, the DOM is unloaded & then reloaded with the new page content. This triggers the closeIt() function.

I need to only trigger the function when the window is closed, or if someone is to leave the domain - so I can make an ajax call & record the log out in the db.

TIA Experts.
0
Comment
Question by:Imaginx
  • 2
  • 2
6 Comments
 
LVL 22

Expert Comment

by:Mrunal
ID: 37723621
Hi,
Try once with JavaScript "window.onclose" event instead of with jQuery.
Here is reference:

https://developer.mozilla.org/en/DOM/window.onclose

Also check this:
http://stackoverflow.com/questions/1631959/browser-window-close-event

Hope this helps you.
0
 
LVL 1

Author Comment

by:Imaginx
ID: 37725497
lol .. I actually read both of those pages in my google searches before posting the question here.

No such luck ..
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37785699
This is such a FAQ.
You need onbeforeunload and you need to set a flag onload and clear it when internal links are clicked - test it onbeforeunload and log them out
In addition your server needs to handle the fact that some users will not trigger your script
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 37785805
Here is an old but still valid post

http://forums.asp.net/t/1094920.aspx/1
0
 
LVL 1

Author Closing Comment

by:Imaginx
ID: 37788549
mplungjan, that's a great link.

apparently theres no perfect solution for what im trying to achieve, but theres enough information in that post for me a come up what a different viable solution.

Thanks !
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

12 Experts available now in Live!

Get 1:1 Help Now