1

I wanted to know if its possible to trigger a popup by just sending the user to a specific url that contains an "#somerandomword" at the end of it.

So normally they click a button on the page to trigger the popup, instead i just want the popup to trigger when the user is given a specific link.

1 Answer 1

3

This could be easily done on page-load, by simply looking for a hash in the URL:

$(document).ready(function(){

    // the #-prefixed portion of the URL that serves
    // as a fragment identifier for the document, from
    // www.example.com/page#fragment
    // document.location.hash would return #fragment
    let hash = document.location.hash;

    // because the hash identifies a fragment of
    // the document by its id, and the '#' character
    // specifies an id-selector in CSS we pass that
    // value to jQuery to select the correct element
    // and call the click() method - without arguments -
    // to trigger a click event:
    $(hash).click();
});

Alternatively, the CSS selector of :target selects the element targeted by the hash, so it's also possible to use:

$(document).ready(function(){
    $(':target').click();
});

References:

Sign up to request clarification or add additional context in comments.

2 Comments

From my understanding this is what is happening. 1. We wait till document loads 2. next we store the # that was at end of url in a variable named hash this is where I get slightly confused. Say my link was www.google.com/puppies#dog we use document.location.hash, and store its value of "#dog" in the variable "hash" so hash now contains the value of "#dog" It seems like there should be some sort of IF statement that says if the value of hash is equal to "#dog" then select the popup button and trigger a click event.
Possibly, but since you never mentioned in your question that you only want to trigger an event on specific fragments/ids that was left out of the answer. Otherwise: yes, that's about what happens.

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.