gpt4 book ai didi

javascript - 当涉及 iframe 时,获取返回按钮以在父页面上工作

转载 作者:行者123 更新时间:2023-11-28 01:14:12 24 4
gpt4 key购买 nike

我正在开发一个涉及 iframe 的遗留应用程序。后退按钮在 iframe 上工作,我需要它绕过 iframe 并仅在父窗口上工作。

这是问题的简化版本和我所知道的描述。

主页“index.html”有一个通过 javascript 添加的 iframe。它加载 a.html,进行 ajax 调用,然后执行 window.location = "b.html"此时,如果您使用后退按钮,它本质上会使 iframe 返回 a.html,然后重定向到 b.html所以你实际上停留在页面上。如果我删除 ajax 调用并在加载时执行 window.location 一切正常。但是,考虑到架构和页面上发生的情况,我无法从图片中删除 Ajax 调用。

这是我正在查看的代码,让我知道您对如何解决此问题的想法。另外我应该提到在 Chrome 41 中这不是问题,但是较新的 chrome 48 和 49 是一个问题。我尝试了 history.replaceState,但无法找到在这种情况下使用它的方法。

index.html

    <html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
</head>
<body>
hello world!
<div id="iframeContainer"></div>

<script>
$(function () {

var newIframe = document.createElement('iframe');
newIframe.src = "a.html";
newIframe.id = "A";

document.getElementById("iframeContainer").appendChild(newIframe);

});
</script>
</body>
</html>

a.html

<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
</head>
<body style="background-color:#F00;">

<script>
$(function(){
$.ajax({
url:"b.html",
complete:function(){
window.location="b.html";
}
});
});
</script>
</body>
</html>

b.html

<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
</head>
<body style="background-color:#00F;">
<script>
$(function(){
})
</script>
</body>
</html>

最佳答案

这只有在 HTML5 兼容的浏览器中才有可能,它会像这样..

这进入子框架..

// catch the back button click.
window.onpopstate = function(event) {
// make the parent window go back
top.history.back();
};

这也只适用于两个框架都在同一域中的情况。

关于javascript - 当涉及 iframe 时,获取返回按钮以在父页面上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36407495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com