iframe src中的JavaScript函数 [英] JavaScript function in iframe src
问题描述
我是JavaScript新手。
I am new to JavaScript.
我有一个javascript函数,它返回我的国家名称。现在我必须将这个值放在iframe的src中。我的JavaScript函数如下:
I have a javascript function which return me country name. Now I have to place this value in the src of iframe. My JavaScript function is as follows:
<script type="text/javascript">
var country = $("#SCountry").val();
function getCountry()
{
var country = $("#SCountry").val();
return country;
}
</script>
我正在获取国家/地区值,但无法将其纳入iframe的src中。我的iframe src如下:
I am getting the country value but can't incorporate it in the src of iframe. My iframe src is as follows:
src='https://example.com/?country="javascript:getCountry()"'
无效。
It's not working.
推荐答案
你可以像这样到达你的iFrame的内容(只要确保给iFrame一个ID)这是一个纯粹的JS解决方案:
You can reach the "content" of your iFrame like this (just be sure to give an ID to the iFrame) This is a pure JS solution:
document.getElementById("iframe_id").contentDocument.getElementById("destination_id").innerHTML = country ;
您的iFrame标记可能如下所示:
< iframe src ='https://domain.com/'id =iframe_id>``< / iframe>
And your iFrame tag could look like:
<iframe src='https://domain.com/' id="iframe_id">``</iframe>
用php:
JS:
`document.getElementById('iframe_id').src = 'https://domain.com/?country=' + country;`
PHP内部iframe:
PHP inside iframe:
$country = $_POST["country"];
echo $country; // this echo you can use where you want the result to show up.
这篇关于iframe src中的JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!