我想知道如何更改網址,而不會像本網站上重定向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。 –