You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
navigate: function(fragment,options){if(!History.started)returnfalse;if(!options||options===true)options={trigger: !!options};// Normalize the fragment.fragment=this.getFragment(fragment||'');// Don't include a trailing slash on the root.varroot=this.root;if(fragment===''||fragment.charAt(0)==='?'){root=root.slice(0,-1)||'/';}varurl=root+fragment;// Strip the hash and decode for matching.fragment=this.decodeFragment(fragment.replace(pathStripper,''));if(this.fragment===fragment)return;this.fragment=fragment;// If pushState is available, we use it to set the fragment as a real URL.if(this._usePushState){this.history[options.replace ? 'replaceState' : 'pushState']({},document.title,url);// If hash changes haven't been explicitly disabled, update the hash// fragment to store history.}elseif(this._wantsHashChange){this._updateHash(this.location,fragment,options.replace);if(this.iframe&&(fragment!==this.getHash(this.iframe.contentWindow))){variWindow=this.iframe.contentWindow;// Opening and closing the iframe tricks IE7 and earlier to push a// history entry on hash-tag change. When replace is true, we don't// want this.if(!options.replace){iWindow.document.open();iWindow.document.close();}this._updateHash(iWindow.location,fragment,options.replace);}// If you've told us that you explicitly don't want fallback hashchange-// based history, then `navigate` becomes a page refresh.}else{returnthis.location.assign(url);}if(options.trigger)returnthis.loadUrl(fragment);},
start: function(options){if(History.started)thrownewError('Backbone.history has already been started');History.started=true;// Figure out the initial configuration. Do we need an iframe?// Is pushState desired ... is it available?this.options=_.extend({root: '/'},this.options,options);this.root=this.options.root;this._wantsHashChange=this.options.hashChange!==false;this._hasHashChange='onhashchange'inwindow;this._useHashChange=this._wantsHashChange&&this._hasHashChange;this._wantsPushState=!!this.options.pushState;this._hasPushState=!!(this.history&&this.history.pushState);this._usePushState=this._wantsPushState&&this._hasPushState;
.....
基于backbone h5 history 的使用
backbone h5 history 的代码
先来看下 backbone的backbone.route.navigate 代码源码
backbone 是否使h5方式还是hash方式的条件是this._usePushState==true
在来看this._usePushState的定义函数是backbone.history.start 函数
可以看出,是否启用pushstate是有当前页面是否支持pushstate和页面调用backbone.history.start函数时,是否指定使用pushstate一起来决定的。
使用backbone的H5 history
1.开启backbone route的使用pushstate方式
2. 配置route表
3.执行页面的跳转
trigger:true的配置项,是为了让页面的地址栏变化的同时,也改变页面的内容展示,否则只改变页面地址,不改变内容。
4.如果当前页面是一个tab页
每个tab页都有独立的地址,每次切换tab都会留下历史记录,使得页面的后退,陷入循环中,为了处理这个问题,需要设置replace:true来解决。
5. 页面跳转的简单封装
为了实现统一跳转入口,实现以上的2组跳转,可以简单封装一个函数
以后每次调用,只要简单的把需要跳转的地址传入就好,如果是tab页,也只要把第二个参数设置为true
The text was updated successfully, but these errors were encountered: