history.pushState 错误,使用 fancybox “无法在'历史'上执行'pus

history.pushState error using fancybox "Failed to execute 'pushState' on 'History': A history "

本文关键字:无法在 历史 pus 执行 错误 pushState 使用 fancybox history      更新时间:2023-09-26

我正在使用' fancy box ',我正在尝试将我的网址
更改为fancy box的网址,所以我尝试使用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历史。推送状态不起作用?但我从答案中不明白我的代码中有什么问题)

编辑:
另一个问题是,当我在浏览器中按"返回"时,URL 已更改但未实际加载,为什么?

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

查看您的错误消息,我猜您的源服务器 http://localhost:28587。无论如何,回答我的评论,您说您尝试使用源 http://localhost:27477 推送 url。正如您所看到的,它们具有不同的端口,从机器的角度来看绝对是不同的。

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