JavaScript 让 iframe 成为父级

javascript let iframe become parent

本文关键字:iframe JavaScript      更新时间:2023-09-26

我的页面上有一个iframe,它与我的网站的域不同。 我知道我无法从父窗口访问 iframe 中的任何内容,但我可以让 iframe 接管我的整个页面 - 即将父级重定向到 iframe 的 URL?

到目前为止,我已经尝试过这样的事情

window.location.href = iframe.contentWindow.location.href;

但是浏览器不允许这样做 - 我想它不够聪明,无法意识到=分配是为了重定向而不是存储以供以后检查。

在所有现代浏览器上是否有另一种方法可以做到这一点?

怎么样:

window.location = iframe.src;

你为什么不尝试使用 iframe src 属性呢?

使用 jquery:

window.location.href = $('#iframeid').attr('src');

或:

window.location.href = document.getElementById('#iframeid').src;