【问题标题】:history.pushState error using fancybox "Failed to execute 'pushState' on 'History': A history "history.pushState 错误使用fancybox“无法在'History'上执行'pushState':历史”
【发布时间】:2015-03-24 20:09:36
【问题描述】:

我正在使用“fancy box”,我正在尝试将我的网址更改为
fancy box 的 url,所以我尝试使用 History.pushState()
但它会出错
错误:

未捕获的安全错误:无法在“历史”上执行“pushState”:无法在来源为“http://localhost:28587”的文档中创建 URL 为“about:blank”的历史状态对象

我的代码:

            var _url = window.location.href;
            setInterval(function () {
                if (($(".fancybox-iframe").length > 0))
                   { 

                if(window.location.href != $('.fancybox-iframe').contents().get(0).location.href)
                {

                    history.pushState(null, null, $('.fancybox-iframe').contents().get(0).location.href);
                } 
                  }
                else 
                {
                  if(window.location.href != _url)
                  { 

                  history.pushState(null,null, _url);
                  } 

                }

            }, 80);

我想搜索它,但没有找到答案,请帮助我
或建议我更好的解决方案。
(我发现了这个:Javascript history.PushState not working? 但我不明白我的代码中有什么问题)

编辑:
另一个问题是,当我在浏览器中按“返回”时,url 发生了变化,但没有实际加载,为什么??

【问题讨论】:

  • 请有人帮帮我!!
  • 记录$('.fancybox-iframe').contents().get(0).location.href 的值并将它们添加到问题中,因为这可能是您的问题的原因
  • 它从 iram 中获取 url,如果我打开谷歌,$('.fancybox-iframe').contents().get(0).location.href 将是http://www.google.com。在我的例子中http://localhost:27477/PerPage/content.php?d=1&f=1 我用警报检查它。另一个问题是,当我在浏览器中按“返回”时,url 已更改但未实际加载,为什么?@GlenSwift

标签: javascript jquery url fancybox-2 pushstate


【解决方案1】:

history.pushState 有安全限制,其中之一是不允许应用程序将跨域 url-s 推送到历史记录。这种明显的行为,你可以阅读更多关于你链接到的问题。

查看您的错误消息,我猜您的源服务器是http://localhost:28587。无论如何,在回答我的评论时,您说您尝试使用来源http://localhost:27477 推送 url。你可以注意到它们有不同的端口,从机器的角度来看肯定是不同的。

我不能向您推荐绝对正确的方法来修复它。这取决于您的应用程序逻辑。首先,您可以使用一个端口来执行您的代码。其次,您可以使用配置为监听两个端口的某种代理(如 nginx)。第三,如果不破坏应用程序的逻辑,您可以推送相对路径 (/PerPage/content.php?d=1&f=1)。尝试使用您可以接受的谷歌解决方案。

【讨论】:

    猜你喜欢
    • 2014-08-17
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 2018-05-06
    • 2015-12-07
    • 1970-01-01
    • 2021-03-28
    • 2019-09-16
    相关资源
    最近更新 更多