Html iframe src 中的 JavaScript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17229245/
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-29 10:19:25  来源:igfitidea点击:

JavaScript function in iframe src

javascripthtml

提问by Muhammad Maaz

I am new to JavaScript.

我是 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:

我有一个返回国家名称的 javascript 函数。现在我必须将此值放在 iframe 的 src 中。我的 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. My iframe src is as follows:

我正在获取国家/地区值,但无法将其合并到 iframe 的 src 中。我的 iframe src 如下:

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

It's not working.

它不起作用。

采纳答案by Sergio

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 src='https://domain.com/' id="iframe_id">``</iframe>

您的 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.

回答by SarathSprakash

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>

回答by M.G.Manikandan

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. Better follow below approach

在上面的方法中,我们最初没有设置 src 属性,因为这个 IE 会安全警告。更好地遵循以下方法

<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 -->

回答by Felix Kling

You cannot simply put JavaScript in an HTML attribute. It won't get evaluated. Instead you have to use JavaScript to manipulate the DOM element, i.e. change its srcproperty.

您不能简单地将 JavaScript 放在 HTML 属性中。它不会被评估。相反,您必须使用 JavaScript 来操作 DOM 元素,即更改其src属性。

<iframe id="myIframe" src=""></iframe>
<script>
    document.getElementById('myIframe').src = 
        'https://domain.com/?country=' + getCountry();
</script>

回答by Ishank

$(function(jQuery){
 var aURL = $('#myIframe').attr( "src");
$('#myIframe').attr( "src",aURL+''+"Isreal"/*use getCountry() here*/);
});

May check this fiddleout

可以检查这个捣鼓