IT & Programming

How to trigger an event with JQuery when leaving the Page

This jquery code snippet trigger an alert event when user moves his mouse towards upper part of the browser like address bar, title bar. Don't forget to add JQuery library before using this function.

Script

$(function(){
	 
	var mouseY = 0;
	
	var topValue = 0;
	
	window.addEventListener("mouseout",function(e){
	
		mouseY = e.clientY;
		
		if(mouseY < topValue) {
			
			alert('do something here');
		}
	},
	
	false);
});

Calling The Script

Script will be called automatically when you move your mouse towards upper part of the browser.

Output

Alert Message 'do something here'

Leave A comment

Email address is optional and will not be published. Only add email address if you want a reply from blog author.
Please fill required fields marked with *