Get the hashchange event to work in all browsers (including IE7)

前端 未结 2 611
予麋鹿
予麋鹿 2021-01-30 15:18

I have some code (written by another developer) that is doing AJAX page loading inside of WordPress (e.g. no page reloads) when you click a nav item, AJAX refreshes the primary

2条回答
  •  一生所求
    2021-01-30 15:28

    attachEvent takes on two params:

    bSuccess = object.attachEvent(sEvent, fpNotify)
    

    [And is needed for all versions of IE below IE9! :( See MDN reference ]

    This could work:

    if(window.addEventListener) {
        window.addEventListener("hashchange", hashChange, false);
    }
    else if (window.attachEvent) {
        window.attachEvent("onhashchange", hashchange);//SEE HERE...
        //missed the on. Fixed thanks to @Robs answer.
    }
    

    Of course if it is possible, you should just use JQuery, since it encapsulates all this for your.

    And as always there is a plugin out there: http://benalman.com/projects/jquery-hashchange-plugin/

提交回复
热议问题