javascriptjquerypageload

Hash URL on pageload in jQuery


I really hope to find a solution here. Need to load specific elements highlighted first on the pages based on url hash. I have already set up "click" and "hover" functions for these elements. But also need these elements highlighted based on url. What selector should I use?

Basically I need the following scenario to be implemented:

if https://mypage.com#case1 loads
do this

if https://mypage.com#case2 loads
do this

Solution

  • If I understand your question, you can get the URL and do a simple if else statement where you load what you need to based on the URL string.

    It could be something like this:

    var url = window.location.href; //get url string
    if(url == "https://mypage.com#case1"){
        //run your case1 code
    }else if(url == "https://mypage.com#case2"){
        //run your case2 code
    }
    

    I'm not sure what your use case is, but you probably want to parse the URL to get the relevant piece or parameter you are looking for.