How to detect whether the button or link will redirect to a different page using jquery?

I got a task to do something whenever my page will get redirect to a different page on click of any link or button. So i need a general event under which i can run my code. I need it throughout the application and can’t give each and every link and button a common class as a selector which will be redirecting a page to a different one since it’s an old app.

$('  ').click(function(

// some code

))

You can add an event listener to the document, and from there check if the clicked element is a link or a button.

I think the question is how to determine if it’s an external link? In this case you might check the .origin property of the anchor element against the window.location.origin:

$('a').filter(function () {
  return this.origin !== window.location.origin
}).click(/* ... */)

PS: Or probably better, check for the .hostname.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.