$(window).bind('popstate',function(event){varstate=event.stateif(state&&state.container){varcontainer=$(state.container)if(container.length){...varoptions={id:state.id,url:state.url,container:container,push:false,fragment:state.fragment,timeout:state.timeout,scrollTo:false}if(contents){// pjax event is deprecated$(document).trigger('pjax',[null,options])container.trigger('pjax:start',[null,options])// end.pjax event is deprecatedcontainer.trigger('start.pjax',[null,options])container.html(contents)pjax.state=statecontainer.trigger('pjax:end',[null,options])// end.pjax event is deprecatedcontainer.trigger('end.pjax',[null,options])}else{$.pjax(options)}...}}}
为了支持fallback, 一个是在加载的时候判断浏览器是否支持history push state
API:
12345
// Is pjax supported by this browser?$.support.pjax=window.history&&window.history.pushState&&window.history.replaceState// pushState isn't reliable on iOS until 5.&&!navigator.userAgent.match(/((iPod|iPhone|iPad).+\bOS\s+[1-4]|WebApps\/.+CFNetwork)/)
另一个是当发现请求一段时间没有回复的时候(可以设置参数timeout),
直接做页面跳转。
123456789
options.beforeSend=function(xhr,settings){if(settings.timeout>0){timeoutTimer=setTimeout(function(){if(fire('pjax:timeout',[xhr,options]))xhr.abort('timeout')},settings.timeout)// Clear timeout setting so jquerys internal timeout isn't invokedsettings.timeout=0