从另一个域的 iframe 中的代理访问父/原始 URL(同源策略)

access parent/original URL from a proxy in iframe from another domain (same origin policy)

本文关键字:URL 原始 策略 另一个 iframe 访问 代理      更新时间:2023-09-26

我有一个带有iframe的页面,该页面在列表中输出第三方内容

每个内容项都在具有唯一类item-Xli

<li class="item-1">
   <a href="item-1.html">View Item 1</a>
</li>
<li class="item-2">
   <a href="item-2.html">View Item 2</a>
</li>

我正在寻找一种方法:

用户访问

www.parent-domain.com/?view_item=1

iframe 读取此地址,并在所引用的item上模拟click

iframe 中的内容使用onLoad('parent-domain.com/foo.html)加载代理 HTML 文件,我可以在其中添加 JS。(大概这是为了绕过同源政策)。

这可能吗?

  • 从父 URL 获取查询字符串
  • 从 iframe 读取值(在 onLoad(); 内的文件中)
  • 在类与值匹配的li a上触发单击事件

我建议你将父查询字符串传递给 iframe(使用服务器端代码等)。

然后,可以轻松地从查询字符串中检索"view_item":

function getParameterByName(name) {
name = name.replace(/['[]/, "'''[").replace(/[']]/, "''']");
var regex = new RegExp("[''?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/'+/g, " "));
}

然后,您只需找到带有id的链接并触发点击:

var link = document.getElementbyId(getParameterByName("view_item"));
link.click();