简体   繁体   English

历史记录API空状态

[英]History API null state

I am trying to change the URL also (without refreshing the page) after I change my DOM with Ajax. 在使用Ajax更改DOM后,我也尝试更改URL(不刷新页面)。

$("nav li").click(function(e) {
    e.preventDefault();
    var url;
    var targetLocation = this.parentNode.attributes.href.value;
    switch (targetLocation) {
    case "index.html": url = "http://localhost:8080/home"; break;
    case "app.html": url = "http://localhost:8080/api"; break;
    case "contact.html": url = "http://localhost:8080/contact"; break;
    }

    $.ajax({
        type: "GET",
        dataType: "text",
        url: url,
        context: document.body,
        success: function(data) {
            $("div").text(data);
            history.pushState("", "", targetLocation);
        }
    })
})

But I get an error that says: 但我收到一条错误消息:

Uncaught DOMException: failed to execute "pushState" on "History": A history state object with URL "file:///C:/test/app.html" cannot be created in a document with origin "null" and URL "file:///C:/test/index.html".

Your code doesn't include them, but your error suggests you're working with local files. 您的代码不包含它们,但是您的错误提示您正在使用本地文件。 Browsers do not support history operations on local filesystem URLs. 浏览器不支持对本地文件系统URL的历史记录操作。

The security model for file:// URLs is broken beyond repair. file:// URL的安全模型已无法修复。 Rather than complicate the browser with special cases for file:// URLs, we've chosen to disable some features for file:// URLs by making a class of security checks fail. 我们没有使File:// URL的特殊情况复杂化浏览器,而是通过使一类安全检查失败来选择禁用File:// URL的某些功能。 Unfortunately, pushState is one of those features. 不幸的是,pushState是这些功能之一。 I'd recommend not using file:// URLs in your application. 我建议您在应用程序中不要使用file:// URL。

https://bugs.chromium.org/p/chromium/issues/detail?id=301210 https://bugs.chromium.org/p/chromium/issues/detail?id=301210

Couldn't find a reference for Firefox, but IIRC their behaviour is the same as Chrome's. 找不到Firefox的参考,但是IIRC的行为与Chrome相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM