javascript - 在 "Failed to execute ' : A history "上使用 fancybox 'History' PushState' 时发生历史记录推送错误

标签 javascript jquery url fancybox-2 pushstate

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

Uncaught SecurityError: Failed to execute 'pushState' on 'History': A history state object with URL 'about:blank' cannot be created in a document with origin 'http://localhost:28587'

我的代码:

            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?,但我不明白我的代码从答案中出了什么问题)

编辑:
另一个问题是,当我在浏览器中按“后退”时,网址会更改但实际上并未加载,为什么??

最佳答案

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

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

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

关于javascript - 在 "Failed to execute ' : A history "上使用 fancybox 'History' PushState' 时发生历史记录推送错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28149680/

相关文章:

jquery - 获取 float 元素的位置

javascript - 使用 Javascript 将 URL 转换为可点击的链接

javascript - 创建需要 JS 模块,主干

javascript - 自定义 cli 生成的feathersjs 服务

javascript - D3库饼图,设置第一个切片的起始 Angular

python - 从谷歌应用引擎点击其他服务器上的 URL

firefox - 以编程方式将 URL 中的 # 传递到浏览器

javascript - Three.js 中的 BoxBufferGeometry 与 BoxGeometry 有什么区别?

javascript - 检查两个特定字符是否在一起

javascript - 如何在 mousedown 事件上更改鼠标光标