简体   繁体   English

iframe src中的JavaScript函数

[英]JavaScript function in iframe src

I am new to JavaScript. 我是JavaScript新手。

I have a javascript function which return me country name. 我有一个返回我的国家名称的JavaScript函数。 Now I have to place this value in the src of iframe. 现在,我必须将此值放在iframe的src中。 My JavaScript function is as follows: 我的JavaScript函数如下:

<script type="text/javascript">
var country = $("#SCountry").val();
function getCountry()
{
   var country = $("#SCountry").val();
   return country;

}
</script>

I am getting the country value but can't incorporate it in the src of iframe. 我正在获取国家/地区价值,但无法将其合并到iframe的src中。 My iframe src is as follows: 我的iframe src如下:

src='https://example.com/?country="javascript:getCountry()"'

It's not working. 没用

This will set the source attribute value of the iframe 这将设置iframe的来源属性值

<script type="text/javascript">
 var country = $("#SCountry").val();
 document.getElementById("iframeid").setAttribute("src","https://domain.com/country="+country)
 </script>

You cannot give javascript code in iframe src attribute directly. 您不能直接在iframe src属性中提供javascript代码。

Instead you can set src of iframe using Javascript like below 相反,您可以使用如下Javascript设置iframe的src

<iframe id="frame"></iframe>

<script>
    window.onload = function() {
        document.getElementById('frame').src = 'https://domain.com/?country=' + getCountry();
    }
</script>

In above approach we didn't set src attribute initially due to this IE will security warning. 在上述方法中,由于此IE将发出安全警告,因此我们最初并未设置src属性。 Better follow below approach 更好地遵循以下方法

<iframe src='/images/spacer.png' onload="this.src = 'https://domain.com/?country=' + getCountry();"></iframe>

<!-- /images/spacer.png meant to be any file. Lesser size is better -->

You cannot simply put JavaScript in an HTML attribute. 您不能简单地将JavaScript放在HTML属性中。 It won't get evaluated. 它不会得到评估。 Instead you have to use JavaScript to manipulate the DOM element, ie change its src property. 相反,您必须使用JavaScript来操作DOM元素,即更改其src属性。

<iframe id="myIframe" src=""></iframe>
<script>
    document.getElementById('myIframe').src = 
        'https://domain.com/?country=' + getCountry();
</script>
$(function(jQuery){
 var aURL = $('#myIframe').attr( "src");
$('#myIframe').attr( "src",aURL+''+"Isreal"/*use getCountry() here*/);
});

May check this fiddle out 可以检查这个小提琴

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: 您可以像这样到达iFrame的“内容”(只需确保为iFrame提供一个ID),这是一个纯JS解决方案:

document.getElementById("iframe_id").contentDocument.getElementById("destination_id").innerHTML = country ;

And your iFrame tag could look like: 您的iFrame广告代码可能看起来像:
<iframe src='https://domain.com/' id="iframe_id">``</iframe>

OR 要么

with php: 与PHP:

JS: JS:

`document.getElementById('iframe_id').src = 'https://domain.com/?country=' + country;`

PHP inside iframe: iframe中的PHP:

$country = $_POST["country"];
echo $country; // this echo you can use where you want the result to show up.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM