0

I would like to trigger a javascript function when a specific URL is available on the page.

The url looks like:

I would like to use jQuery to detect the url and launch an event.

I have come this far:

$( 'a[href="https://www.mypage.com/my-page/details.jsp"]' ).bind( "click", function() {
    alert( "yoehoe" );
});

But it doesn't trigger the alert on a click. on the specific href. Can anyone help me on this one?

1
  • you should wrap it with doc ready. Commented Mar 21, 2014 at 12:17

1 Answer 1

1

You need to wrap the code in document.ready to ensure that event gets bind to respective elements once they are there on page.like this:

$(document).ready(function(){
 $( 'a[href="https://www.mypage.com/my-page/details.jsp"]' ).bind( "click", function() {
   alert( "yoehoe" );
 });});

Demo

Alternatively, You can also use .on() with .click if dom is generated dynamically.like this:

$(document).on("click",'a[href="https://www.mypage.com/my-page/details.jsp"]',function()     {
 alert( "yoehoe" );
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.