简体   繁体   English

浏览器返回在页面本身之前作用于嵌套的 iframe - 有没有办法避免它?

[英]browser back acts on nested iframe before the page itself - is there a way to avoid it?

I have a page with dynamic data loaded by some ajax and lots of javascript.我有一个页面,其中包含一些 ajax 和大量 javascript 加载的动态数据。

the page contains a list from which the user can choose and each selected value loads new data to the page.该页面包含一个列表,用户可以从中进行选择,每个选定的值都会向页面加载新数据。

One of these data items is a url provided to an iframe.这些数据项之一是提供给 iframe 的 url。

I use jQuery BBQ: Back Button & Query Library to simulate the browser-back behavior.我使用jQuery BBQ: Back Button & Query Library来模拟浏览器返回的行为。

All works well besides the fact that when i click the back button for the first time the iframe goes back to its previous location and then I need to click back again to make the page go back.除了当我第一次单击后退按钮时 iframe 返回到其先前位置然后我需要再次单击返回以使页面返回这一事实之外,一切都运行良好。

Is there a way to disable the iframe's back behavior?有没有办法禁用 iframe 的返回行为?

I've found the answer to my problem guess it could be useful for others out there.我找到了我的问题的答案,猜测它可能对其他人有用。

The problem was with the way i assigned new URLs to my Iframe, i used Jquery so it looked something like that:问题在于我为 iframe 分配新 URL 的方式,我使用了 Jquery,所以它看起来像这样:

$('#myIFrame').attr('src',newUrl);

When assigning the URL in that manner it adds a new entry to the browser's list of visited URLs to go back to.当以这种方式分配 URL 时,它会在浏览器的访问 URL 列表中添加一个新条目以返回。
That wasn't the desired behavior, so after some googling i found that you can assign a new URL to an Iframe object without adding it to the 'back-list', it looks like that:这不是我们想要的行为,所以经过一番谷歌搜索后,我发现您可以为 iframe 对象分配一个新 URL,而无需将其添加到“后备列表”中,它看起来像这样:

var frame = $('#myIFrame')[0];  
frame.contentWindow.location.replace(newUrl);

This way my back button behave exactly as expected.这样我的后退按钮的行为完全符合预期。

btw, i got my answer from here .顺便说一句,我从这里得到了答案。

Hope this was helpful to you as it was to me.希望这对你有帮助,就像对我一样。

The accepted answer does not seem to work if you try to set a cross-domain URL for the IFrame.如果您尝试为 IFrame 设置跨域 URL,则接受的答案似乎不起作用。 As a workaround, I detached the IFrame from the DOM before setting the src (using jQuery).作为一种解决方法,我在设置src (使用 jQuery)之前从 DOM 中分离了 IFrame。

// remove IFrame from DOM before setting source, 
// to prevent adding entries to browser history
var newUrl = 'http://www.example.com';
var iFrame = $('#myIFrame');
var iFrameParent = iFrame.parent();

iFrame.remove();
iFrame.attr('src', newUrl);
iFrameParent.append(iFrame);

I suggest you create a hyperlink within your iframe.我建议您在 iframe 中创建一个超链接。 call it 'Back' and put a href as javascript:history.back(-1)That's the best you can do I think.将其称为“返回”并将 href 设为 javascript:history.back(-1) 我认为这是您能做的最好的事情。

基本上你需要防止在 iframe 中添加新的历史条目,HTML5 中引入的history.replaceState在大多数情况下都可以工作。

history.pushState(state, title, url);

I manage cross domain iframe inside bootstrap modal, the only solution working for me is to put the following code inside the head of the each iframe pages:我在引导模式中管理跨域 iframe,对我有用的唯一解决方案是将以下代码放在每个 iframe 页面的头部:

    history.pushState(null, null, location.href);
    window.onpopstate = function () {
      history.go(1);
    };

This will not work for everyone as you need to have the control of the iframe content, in addition it break the history chain of the parent window这对每个人都不起作用,因为您需要控制 iframe 内容,此外它还打破了父窗口的历史链

不,这完全取决于浏览器。

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

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