Solved

jQuery on Window Close

Posted on 2012-03-14
6
444 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

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
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)

813 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

11 Experts available now in Live!

Get 1:1 Help Now