Last active
December 19, 2015 07:09
-
-
Save Sleavely/5917127 to your computer and use it in GitHub Desktop.
Run an AJAX call before leaving the page through a link.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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