Solved

jQuery on Window Close

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generate Numbers in JQuery file 11 66
How to display div value on input field value in php 11 53
jQuery Dialog Autoresize Bug 2 24
Json Schema 8 32
This article discusses four methods for overlaying images in a container on a web page
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

840 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