更改网址而无需使用javascript重定向

kb8*_*858 74 javascript ajax jquery redirect

我想知道如何在没有重定向的情况下更改网址,如 我们点击标签页面http://dekho.com.pk/ads-in-lahore,网页剂量完全重新加载.stackoverflow还有其他问题表明它是不可能的,但我想知道上面提到的网站是如何实现它的.谢谢

Mih*_*rga 161

用途pushState:

window.history.pushState("", "", '/newpage');
Run Code Online (Sandbox Code Playgroud)

  • `window.history.replaceState(data,title,url);`也很有用 (4认同)
  • https://developer.mozilla.org/en-US/docs/Web/API/History_API (3认同)
  • 哇,这就像一个魅力! (2认同)

Jar*_*ish 8

如果你想确切知道他们使用的是什么,那就是Backbone.js(见行45744981).它们都与jQuery源混合在一起,但这些是带注释的Backbone.Router文档页面的相关行:

支持的检查:

  this._wantsPushState = !!this.options.pushState;
  this._hasPushState = !!(this.options.pushState && window.history && window.history.pushState);
Run Code Online (Sandbox Code Playgroud)

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;
},
Run Code Online (Sandbox Code Playgroud)

hash和push状态之间进行选择:

// 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);
}?
Run Code Online (Sandbox Code Playgroud)

更多关于他们的目标:

// 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();
Run Code Online (Sandbox Code Playgroud)

政变"d恩典:

// 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);
}
Run Code Online (Sandbox Code Playgroud)

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