How to run a JavaScript function when the user is visiting an hash link (#something) using JQuery?

左心房为你撑大大i 提交于 2019-12-08 01:30:26

Here is something I do:

    var hash = (window.location.hash).replace('#', '');
    if (hash.length == 0) {
        //no hash
    }
    else {
        //use `hash`
        //example:
        if(hash == 'add-item'){
             //do something
        }
    }

Might be able to use the hashchange event, as shown at http://benalman.com/projects/jquery-hashchange-plugin/.

$(document).ready(
    function() {
        $('#search').keyup(
            function() { ... }
        );

        $('#add-item').click(
            function() { $("#content").html(myForm); }
        );
    }
);

I assume you have a element with id "content" where you want to display the form.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!