2012-09-16 70 views
44

我想知道如何更改网址,而不会像本网站上重定向http://dekho.com.pk/ads-in-lahore 当我们点击标签URL更改,但页面dosent完全重新加载。在stackoverflow上还有其他问题表明这是不可能的,但我想知道上面提到的网站是如何实现它的。 感谢更改网址,而无需使用javascript重定向

+0

在您提到的链接上,调用了相同的webservice,但参数不同。 –

+0

['history.pushState()'](https://developer.mozilla.org/en-US/docs/DOM/Manipulating_the_browser_history)。那里也有jQuery,但是这个特定的技术已经成为了。为旧版浏览器推回状态。 –

+0

您也可以使用window.location.hash并修改http://url.com/page#hash。 –

回答

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的(见线45744981)。这一切都混合起来在那里与jQuery的来源,但这些都是annotated Backbone.Router source文档页面的相关线路:

support checks

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(); 

而且coup 'd grace

// 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的链接我在上面提供。非常丰富。

+0

谢谢您的回答。它提供的信息非常丰富。我不知道骨干,所以我要去推动它,因为它满足我所有的需求。 – kb858

+0

Backbone.js是围绕它的路由器构建的,因为它是一个“单页应用程序”。虽然你可能还没有足够的经验来看我在上面的回答,那就是* Backbone.js正在做的事情,以及优雅的退化(对于那些没有支持它的浏览器的人)。在你的问题下查看我的链接我会使用像https://github.com/balupton/History.js/这样的库,而不是自己推出。除非你知道你在做什么。 –

+0

我使用History.js,因为我是一个新手:) – kb858

相关问题