javascript - window.onpopstate on page load -


i'm playing window.onpopstate, , there thing annoys me little bit:

browsers tend handle popstate event differently on page load. chrome , safari emit popstate event on page load, firefox doesn't.

source

i tested it, , yeah, in chrome , safari 5.1+ popstate event fired on page load, not in firefox or ie10.

the problem is, want listen popstate events user clicked back or forward button (or history changed via javascript), don't want on pageload.

by other words want differentiate popstate event page load other popstate events.

this tried far (i'm using jquery):

$(function() {   console.log('document ready');    settimeout(function() {     window.onpopstate = function(event) {       // here     }, 10); }); 

basically try bind listener function popstate late enough not bound on page load, later.

this seems work, however, don't solution. mean, how can sure timeout chosen settimeout big enough, not big (because don't want wait much).

i hope there smarter solution!

check boolean truth of event.state in popstate event handler:

window.addeventlistener('popstate', function(event) {     if (event.state) {         alert('!');     } }, false); 

to ensure work, always specify non-null state argument when calling history.pushstate() or history.replacestate(). also, consider using wrapper library history.js provides consistent behavior across browsers.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -