I\'m trying to write a Cross-Browser script that detects when a link is clicked on a page (text link, image, or othwerwise) so that I can show a message or ad (like an inter
To call a function whenever a link—dynamically added or not—has been clicked, use on()
$(document).on("click", "a", function() {
//this == the link that was clicked
var href = $(this).attr("href");
alert("You're trying to go to " + href);
});
If you're using an older version of jQuery, then you would use delegate() (note that the order of selector and event type is switched)
$(document).delegate("a", "click", function() {
//this == the link that was clicked
var href = $(this).attr("href");
alert("You're trying to go to " + href);
});