在不使用javascript重定向的情况下更改url

在不使用javascript重定向的情况下更改url,javascript,jquery,ajax,redirect,Javascript,Jquery,Ajax,Redirect,我想知道如何在不重定向的情况下更改url,就像在这个网站上一样 当我们点击标签时,url会改变,但页面会完全重新加载。关于stackoverflow还有其他问题表明这是不可能的,但我想知道上述网站是如何实现的。 谢谢使用pushState: window.history.pushState("", "", '/newpage'); 如果您想确切地知道他们使用的是什么,可以使用Backbone.js(请参见第4574行和4981行)。它与jQuery源代码混在一起,但文档页面的相关行如下: 委员

我想知道如何在不重定向的情况下更改url,就像在这个网站上一样 当我们点击标签时,url会改变,但页面会完全重新加载。关于stackoverflow还有其他问题表明这是不可能的,但我想知道上述网站是如何实现的。
谢谢

使用
pushState

window.history.pushState("", "", '/newpage');

如果您想确切地知道他们使用的是什么,可以使用Backbone.js(请参见第
4574行和
4981行)。它与jQuery源代码混在一起,但文档页面的相关行如下:

委员会:

路由
功能:

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;
},
在以下选项中进行选择:

更多关于他们在做什么的信息:

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


您应该阅读我在顶部提供的带注释的Backbone.js链接。非常有用。

在您提到的链接上,调用了相同的Web服务,但参数不同。。这里也有jQuery,但必须使用这种特殊的技术。对于较旧的浏览器,具有回退功能的推送状态。您还可以使用window.location.hash和modify。似乎是对
pushState
技术的相当全面的理解。请参阅。谢谢你的回答。内容非常丰富。我还不知道主干网,所以我选择pushstate,因为它满足了我的所有需求。backbone.js是围绕路由器构建的,因为它是一个“单页应用程序”。虽然您可能还没有足够的经验在我上面的回答中看到它,但这正是Backbone.js所做的,而且它的性能也有良好的下降(对于那些没有支持它的浏览器的用户)。请参阅您问题下评论中的我的链接;我会用一个图书馆,而不是你自己的。除非你知道你在做什么。我正在使用History.js,因为我是一个新手:)参考链接了解更多详细信息
window.History.replaceState(数据、标题、url)也很有用哇,这就像一个魅力!在firefox中不起作用-“SecurityError:操作不安全。”
// 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);
}