如何在 Javascript 中将属性值从 iframe 传递给父级?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3381920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:22:37  来源:igfitidea点击:

How can I pass attribute value from iframe to parent in Javascript?

javascriptiframe

提问by nandu3685

How can I pass attribute value from iframe to parent?

如何将属性值从 iframe 传递给父级?

I.e. iframe (frame1) has <a id="a1" href="http://www.infinitybusiness.net">. How can I get value of hrefin my parent page?

即 iframe (frame1) 有<a id="a1" href="http://www.infinitybusiness.net">. 如何href在父页面中获取值?

采纳答案by DuduAlul

This is a great tutorial that talks exactly about that: http://www.roseindia.net/java/pass-value-example/pass-value-from-iframe-parent.shtml

这是一个很好的教程,正好讨论了这个问题:http: //www.roseindia.net/java/pass-value-example/pass-value-from-iframe-parent.shtml

you can use it only if the two pages are served from the same domain. There are many solution for cross-domain scripting as well..you can read more here: http://softwareas.com/cross-domain-communication-with-iframes

只有当两个页面来自同一个域时,您才能使用它。跨域脚本也有很多解决方案..你可以在这里阅读更多:http://softwareas.com/cross-domain-communication-with-iframes

回答by Jhong

var fra = document.getElementById('frameId');
// following will work on same domain (or subdomain with document.domain set) only
var fraContent = fra.contentDocument || fra.contentWindow.document;
myLinkHref = fraContent.getElementById('myLinkId').href;

If this is cross-domain, then you will need to have access to add script to the child frame. If not, it can't be done without a server-side proxy.

如果这是跨域的,那么您将需要有权将脚本添加到子框架。如果没有,则没有服务器端代理就无法完成。

回答by user3682349

it is easy:

这很容易:

  • you should use %26 instead of & to send to iframe
  • 您应该使用 %26 而不是 & 发送到 iframe

for instance: Link: 2.asp?ppp=/Payam.asp?sid=1%26PID=1&sa=12313in Code: when i requset.queryString("ppp")in the parent it shows: /Payam.asp?sid=1&PID=1

例如链接2.asp?ppp=/Payam.asp?sid=1%26PID=1&sa=12313在代码中:当我requset.queryString("ppp")在父级中时,它显示: /Payam.asp?sid=1&PID=1

you can use this as source of your iframe , it has two parameter

您可以将其用作 iframe 的来源,它有两个参数

<IFRAME name="My"  src='<%=ppp %>'></IFRAME>