我想知道如何更改网址,而不会像本网站上重定向http://dekho.com.pk/ads-in-lahore 当我们点击标签URL更改,但页面dosent完全重新加载。在stackoverflow上还有其他问题表明这是不可能的,但我想知道上面提到的网站是如何实现它的。 感谢更改网址,而无需使用javascript重定向
44
A
回答
113
使用pushState
:
window.history.pushState("", "", '/newpage');
+0
参考链接了解更多详情http://diveintohtml5.info/history.html –
+0
https://developer.mozilla.org/en-US/docs/Web/API/History_API –
6
如果你想使用它们到底是什么就知道了,它是Backbone.js的(见线4574
和4981
)。这一切都混合起来在那里与jQuery的来源,但这些都是annotated Backbone.Router
source文档页面的相关线路:
this._wantsPushState = !!this.options.pushState;
this._hasPushState = !!(this.options.pushState && window.history && window.history.pushState);
的route
功能:
route: function(route, name, callback) {
Backbone.history || (Backbone.history = new History);
if (!_.isRegExp(route)) route = this._routeToRegExp(route);
if (!callback) callback = this[name];
Backbone.history.route(route, _.bind(function(fragment) {
var args = this._extractParameters(route, fragment);
callback && callback.apply(this, args);
this.trigger.apply(this, ['route:' + name].concat(args));
Backbone.history.trigger('route', this, name, args);
}, this));
return this;
},
之间进行选择hash and push state s:
// Depending on whether we're using pushState or hashes, and whether
// 'onhashchange' is supported, determine how we check the URL state.
if (this._hasPushState) {
Backbone.$(window).bind('popstate', this.checkUrl);
} else if (this._wantsHashChange && ('onhashchange' in window) && !oldIE) {
Backbone.$(window).bind('hashchange', this.checkUrl);
} else if (this._wantsHashChange) {
this._checkUrlInterval = setInterval(this.checkUrl, this.interval);
}
更多关于他们在做什么:
// If we've started off with a route from a `pushState`-enabled browser,
// but we're currently in a browser that doesn't support it...
if (this._wantsHashChange && this._wantsPushState && !this._hasPushState && !atRoot) {
this.fragment = this.getFragment(null, true);
this.location.replace(this.root + this.location.search + '#' + this.fragment);
// Return immediately as browser will do redirect to new url
return true;
// Or if we've started out with a hash-based route, but we're currently
// in a browser where it could be `pushState`-based instead...
} else if (this._wantsPushState && this._hasPushState && atRoot && loc.hash) {
this.fragment = this.getHash().replace(routeStripper, '');
this.history.replaceState({}, document.title, this.root + this.fragment);
}
if (!this.options.silent) return this.loadUrl();
// If pushState is available, we use it to set the fragment as a real URL.
if (this._hasPushState) {
this.history[options.replace ? 'replaceState' : 'pushState']({}, document.title, url);
}
你应该阅读注释Backbone.js的链接我在上面提供。非常丰富。
相关问题
- 1. 从yii2更改导航栏网址而无需重定向
- 2. 重定向url而不更改网址
- 3. .htaccess:无需更改网址即可重定向
- 4. 域名重定向无需更改网址
- 5. 更改网址而不更改实际路径重定向
- 6. 使用.htaccess进行重定向而不更改地址栏中的网址
- 7. htaccess更改网址,不要重定向
- 8. 更改WordPress网址结构 - 重定向
- 9. $ location.path更改网址但不重定向
- 10. 更改Facebook Oauth重定向网址
- 11. 301重定向到更改的网址
- 12. 更改网址而无需重新加载
- 13. 更改网址而无需重新加载页面
- 14. jQuery - 如何更改网址而无需重新加载?
- 15. Twitter如何更改网址而无需重新加载页面?
- 16. 仅在不重定向网页的情况下使用javascript更改网址?
- 17. htaccess重定向页面而不更改网址
- 18. 重定向页面从一个页面到另一个页面,而无需使用htaccess更改网址
- 19. 将WordPress网址重定向到父页面而没有更改网址
- 20. 如何使用ajax发送当前网址而无需重定向?
- 21. mod_rewrite一个网址到另一个网址,而无需更改源网址
- 22. .htaccess重定向无需更改地址栏
- 23. 将网站从网站A重定向到网站B,而不更改网址
- 24. 设计omniauth重定向到根网址,我无法更改它
- 25. 如何更改用户浏览器中的网址,而无需在servlet中重定向客户端
- 26. htaccess重定向url无需在地址栏中链接网址
- 27. 将domain.com重定向到domain.com/category而不更改地址栏中的网址
- 28. 更改网址并更新页面内容而无需重新加载
- 29. .htaccess重写而不更改网址
- 30. 设置网址重定向到一个特定的网址,而不用重写
在您提到的链接上,调用了相同的webservice,但参数不同。 –
['history.pushState()'](https://developer.mozilla.org/en-US/docs/DOM/Manipulating_the_browser_history)。那里也有jQuery,但是这个特定的技术已经成为了。为旧版浏览器推回状态。 –
您也可以使用window.location.hash并修改http://url.com/page#hash。 –