Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jQuery on Window Close

Posted on 2012-03-14
6
Medium Priority
?
457 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
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 …

715 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