本文介绍了使用fancybox的"history.pushState错误"“无法在“历史记录"上执行"pushState":历史记录".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用'fancy box',并且我试图将我的网址更改为
fancy box的网址,所以我尝试使用History.pushState().
但这会导致错误
错误:

I'm using 'fancy box' and I'm trying to change my url to be
the url of the fancy box, so I try to use History.pushState().
but it makes error
The error:

我的代码:

            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历史记录.PushState无法正常工作?,但我不明白是什么错误在我的答案中的代码中)

I'd search it and didn't find an answer, please help me
or Suggest me better solution for doing that.
(I found this:Javascript history.PushState not working? but I didnt understand what wrong in my code from the answer)

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


and another problem is that when i press "back" in browser the url is change but not actually loaded, why??

推荐答案

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

history.pushState has security restrictions and one of them is not to allow application to push cross-origin url-s to history. This obvious behavior and you can read more about it in question that you link to.

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

Looking at your error message I guess that your origin server is http://localhost:28587. Anyway, answering my comment you say that that you try to push url with origin http://localhost:27477. As you can notice they have different ports and from machine's point of view is definitely different.

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

I can not recommend you absolutely right way to fix it. It depends on your application logic. First you can use one port to execute your code. Second you can use some kind of proxy (like nginx) configured to listen both ports. Third, you can push relative path (/PerPage/content.php?d=1&f=1) if it is not break your application's logic. Try to google solution acceptable for you.

这篇关于使用fancybox的"history.pushState错误"“无法在“历史记录"上执行"pushState":历史记录".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 00:48