HTML5 onpopstate on page load

前端 未结 6 1794
一整个雨季
一整个雨季 2020-12-06 06:18

I\'m using the new HTML5 onpopstate event. Using Firefox 4 the window.onpopstate event is triggered on a page load whilst in Webkit this does not seem to be the case.

<
6条回答
  •  心在旅途
    2020-12-06 06:59

    Just solved this issue, and the fix is actually quite simple. Firefox doesn't fire onpopstate on initial page load. After you fire a popstate, return false or prevent default on the page.load action, and you're golden.

    Cheers.

    In action here: www.thecoffeeshopnyc.com

    // Write in a new handler for Firefox, which does not fire popstate on load.
    // Modern Webkit browsers fire popstate on load. The event handler below fires the controller
    $(window).load(function () {
        if ($.browser.mozilla) {
            // Your func.
        }
    })
    
    // Add the event listener. If the above is false, it will still run as intended in normal browsers.
    window.onpopstate = function() {
        // Your func.
    }
    

提交回复
热议问题