Jquery - tell when the hash changes?

后端 未结 5 958
野趣味
野趣味 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

    You should have a look at the solution of Ben Nadel which is in binding events to non-DOM objects.

    0 讨论(0)
  • 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);
        }
    }
    
    0 讨论(0)
  • 2021-01-03 15:40

    You can try History Event plugin.

    0 讨论(0)
  • 2021-01-03 15:55

    Ben 'the cowboy' Alman wrote a cross platform plugin for hash changes

    http://benalman.com/news/2010/07/jquery-hashchange-event-v13/

    I dont know if your using it inside of the iframe or what, but if you were to use it outside the iframe it would be like

    $(function(){
        $(window).hashchange(function(){
           //Insert event to be triggered on has change.
           changeIframeContent(window.location.hash);
        })
    })
    
    0 讨论(0)
  • 2021-01-03 16:00

    After the document is ready, you examine the hash and alter the page appropriately.

    I don't know the hook for jQuery to be able to tell when the hash changes

    You can intercept your hash anchors' click events and respond appropriately as opposed to waiting for a "the hash has changed" event (which doesn't exist).

    Some approaches create a "the hash has changed" event by inspecting window.location.hash on a timer.

    0 讨论(0)
提交回复
热议问题