Jquery - tell when the hash changes?

后端 未结 5 961
野趣味
野趣味 2021-01-03 15:29

I\'m trying to make my site respond correctly to the back button. I\'ve saved that hash of what I want it to do, but I don\'t know the hook for jQuery to be able to tell wh

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-03 15:36

    There is'nt a buitin way to watch for hash changes, in firefox you could use watch method, but as far as I know it isnt default, so you can hack it writing something like (see below)

    function setWatchHashChanges(functionObject)
    {
        if(window.location.watch)
        {
            window.location.watch('hash', function(e){functionObject(e);return e;});
        } else {
            if(!setWatchHasnChanges.hash)
            {
                setWatchHasnChanges.hash = window.locaton.hash;
            } else {
                setInterval(function(){
                if(setWatchHasnChanges.hash!== window.locaton.hash)
                {
                    setWatchHasnChanges.hash = window.locaton.hash;
                    functionObject(setWatchHasnChanges.hash);
                }
            }, 100);
        }
    }
    

提交回复
热议问题