[英]How to access iframe into parent window from iframe itself?
I have a top document like this (top and iframes are same origin) : 我有一个像这样的顶级文档(top和iframe的来源相同):
<html>
<body>
<iframe class="loaded" src="https://www.example.com/article/645114"/>
<iframe class="loaded" src="https://www.example.com/article/184789"/>
<iframe class="loaded" src="https://www.example.com/article/4325"/>
<iframe class="loaded" src="https://www.example.com/article/32586"/>
</body>
</html>
If a script runs in https://www.example.com/article/4325
I would like to know : 如果脚本在https://www.example.com/article/4325
运行,我想知道:
"This is running from frame 3"
The same script running in https://www.example.com/article/645114
would return : 在https://www.example.com/article/645114
运行的相同脚本将返回:
"This is running from frame 1"
How shoud I do ? 我该怎么办?
Thank you 谢谢
Try the following in the script running in the iframe: 在iframe中运行的脚本中尝试以下操作:
var loadeds = top.document.querySelectorAll('.loaded');
for(var i = 0; i < loadeds.length; i++){
var src = loadeds[i].getAttribute('src'),
this_url = document.URL;
if(src === this_url){
console.log('This is running from frame ' + (i + 1) + ' with the src attribute as ' + this_url);
// Alternatively you could use.
// alert('This is running from frame ' + (i + 1) + ' with the src attribute as ' + this_url);
}
}
Didn't get to try it, but if you run this script in an iframe with your HTML structure above, you should get a console message telling you which frame the script is running in. 不必尝试,但是如果您在上面具有HTML结构的iframe中运行此脚本,则应该收到一条控制台消息,告诉您脚本在哪个框架中运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.