How to execute a js function based on url hash url#nameoffunction

后端 未结 5 1230
执念已碎
执念已碎 2021-01-02 09:08

I saw some of websites executes a JavaScript function based on has in the URL. For example,

when I access http://domain.com/jobs#test

then the website execut

5条回答
  •  隐瞒了意图╮
    2021-01-02 09:45

    This is what i do:

    window.onload = function(){
        var hash = (window.location.hash).replace('#', '');
        if (hash.length == 0) {
            //no hash do something
        }
        else {
            //else do something with hash
        }
    }
    

    demo: http://jsfiddle.net/maniator/XCjpy/show/#test
    demo2: http://jsfiddle.net/maniator/XCjpy/show/
    demo3: http://jsfiddle.net/maniator/XCjpy/show/#testing_again

提交回复
热议问题