Jquery - tell when the hash changes?

点点圈 提交于 2019-11-30 16:29:20

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.

You can try History Event plugin.

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);
    })
})

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

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