将 javascript 值传入 iframe 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2625615/
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
passing in javascript values into iframe tag
提问by Cedar Jensen
What's the best way to pass in the value held in a javascript variable into an iframe call on the same html page? I'm trying to improve my site's page response times by moving ad serving javascript code (the typical document.write('<script type="text/javascript" src="..") into a separate iframe. (Based on this posting)
将 javascript 变量中保存的值传递到同一 html 页面上的 iframe 调用的最佳方法是什么?我试图通过将广告服务 javascript 代码(典型的document.write('<script type="text/javascript" src="..")移动到单独的 iframe 中来改善我网站的页面响应时间。(基于此贴)
The request to the ad server typically require a seed variable declared once per site and incremented each time page is loaded by the client. What I want to do is pass in the seed variable into the document invoked by my iframe section.
对广告服务器的请求通常需要为每个站点声明一次的种子变量,并且每次客户端加载页面时都会增加。我想要做的是将种子变量传递到 iframe 部分调用的文档中。
The seed variable is initialized in the 'head' tag of my main html document:
种子变量在我的主 html 文档的“head”标签中初始化:
<head>
<script type="text/javascript">
<!--
custom_seed=1;
//-->
</script>
</head>
Later in the html document, I make the request through an iframe which returns the html necessary to invoke the ad server.
稍后在 html 文档中,我通过 iframe 发出请求,该 iframe 返回调用广告服务器所需的 html。
<body>
<!-- a bunch of html to display the page -->
<iframe src="somepage.html" width="100%" height="100%">
<p>No support for iframe</p>
</iframe>
</body>
The html returned in the 'somepage.html' has a script used to call the ad server and needs to use the earlier declared seed variable as a parameter:
'somepage.html' 中返回的 html 有一个用于调用广告服务器的脚本,需要使用之前声明的种子变量作为参数:
<script type="text/javascript">
document.write('<script type="text/javascript" src="http://ad.server.net/...seed='+ custom_seed +'?"></script>');
custom_seed++;
</script>
What's a good way to achieve this?
实现这一目标的好方法是什么?
回答by tloflin
The onlyway you can pass values directly to an iframe of a page with a different domain is through the url, as querystring values, anchors, or perhaps some form of rewriting. Even if it's on the same domain, that's probably the easiest and safest way to pass it.
将值直接传递给具有不同域的页面的 iframe的唯一方法是通过 url,作为查询字符串值、锚点或某种形式的重写。即使它在同一个域中,这也可能是最简单、最安全的传递方式。
Main document:
主要文件:
document.getElementById('IframeID').src = "somepage.html?seed=" + custom_seed;
Inner document:
内部文件:
var seed = window.location.search.substring(window.location.search.indexOf('seed=') + 5);
if (seed.indexOf('&') >= 0) {
seed = seed.substring(0, seed.indexOf('&'));
}
回答by Squilo
A simple away to get the value in iframe file :
获取 iframe 文件中的值的简单方法:
document.getElementById("youfiled").value="";
And use the echo php $_GET['seed'];
并使用echo php $_GET['seed'];
inside of the coma element;
彗形元件内部;
回答by DJ Martin
Here's a neat workaround with no ugly url variables. While you can't send variables into an iframe, you can call a function to run.
这是一个简洁的解决方法,没有丑陋的 url 变量。虽然您不能将变量发送到 iframe,但您可以调用一个函数来运行。
Begin by programming a function in the destination page to receive and set the variable:
首先在目标页面中编写一个函数来接收和设置变量:
<script type="application/javascript">
function variable(value) {
var seed = value;
</script>
Then all you need to do is call the function from the origin page:
然后您需要做的就是从原始页面调用该函数:
<script type="application/javascript">
document.getElementById("IframeID").contentWindow.variable("value");
</script>

