Skip to content

Instantly share code, notes, and snippets.

@Sleavely
Last active December 19, 2015 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sleavely/5917127 to your computer and use it in GitHub Desktop.
Save Sleavely/5917127 to your computer and use it in GitHub Desktop.
Run an AJAX call before leaving the page through a link.
jQuery(".my-awesome-link-class").one('click', function(event){
var $this = jQuery(this);
//no need to delay when the link opens in a new window
if (!$this.attr("target") && !event.ctrlKey && !event.shiftKey && !event.metaKey) {
event.preventDefault();
var newHref = this.href;
//the below is pseudo-code
jQuery.ajax({
success: function(){
//now go to the URL that the user intended
document.location.href = newHref;
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment