如何从父html通过iframe传递参数?

时间:2022-06-01 15:15:09

I have a html page in which I am setting the src for an iframe programmatically. How can I pass parameters through the iframe src and get them in the child html?

我有一个html页面,我在其中以编程方式为iframe设置src。如何通过iframe src传递参数并在子html中获取它们?

Below my code:

在我的代码下面:

<iframe id="myIframe" src="" height="250px"  width="100%" scrolling="yes" frameborder="0"></iframe>

function myFunction(){
$('#myIframe').attr('src', "myIframeRequest.html"); 
}

3 个解决方案

#1


18  

On the main page simply pass parameters as follows

在主页面上只需传递参数如下

function myFunction(){
$('#myIframe').attr('src', "myIframeRequest.html?param1=value1&param2=value2"); 
} 

In Iframe

在Iframe中

You can use a script to get the desired parameter value from parameters passed to page.

您可以使用脚本从传递给页面的参数中获取所需的参数值。

<script>
function getParamValue(paramName)
{
    var url = window.location.search.substring(1); //get rid of "?" in querystring
    var qArray = url.split('&'); //get key-value pairs
    for (var i = 0; i < qArray.length; i++) 
    {
        var pArr = qArray[i].split('='); //split key and value
        if (pArr[0] == paramName) 
            return pArr[1]; //return value
    }
}
</script>

Then you can fetch the value of desired parameter like this

然后你可以像这样获取所需参数的值

var param1 = getParamValue('param1');

#2


2  

If you have slightly more control on your iframe sandbox, you can try postMessage API to communicate with message on events you desire to trigger.

如果您对iframe沙箱拥有更多控制权,则可以尝试使用postMessage API与您希望触发的事件上的消息进行通信。

#3


1  

you should make a serverside page like (myIframeRequest.php) php/jsp and get the parameter value in that page if it s an html page then parse the window.location.href through javascript and find the query/param

你应该创建一个像(myIframeRequest.php)php / jsp这样的服务器端页面并获取该页面中的参数值,如果它是一个html页面然后通过javascript解析window.location.href并找到查询/参数

#1


18  

On the main page simply pass parameters as follows

在主页面上只需传递参数如下

function myFunction(){
$('#myIframe').attr('src', "myIframeRequest.html?param1=value1&param2=value2"); 
} 

In Iframe

在Iframe中

You can use a script to get the desired parameter value from parameters passed to page.

您可以使用脚本从传递给页面的参数中获取所需的参数值。

<script>
function getParamValue(paramName)
{
    var url = window.location.search.substring(1); //get rid of "?" in querystring
    var qArray = url.split('&'); //get key-value pairs
    for (var i = 0; i < qArray.length; i++) 
    {
        var pArr = qArray[i].split('='); //split key and value
        if (pArr[0] == paramName) 
            return pArr[1]; //return value
    }
}
</script>

Then you can fetch the value of desired parameter like this

然后你可以像这样获取所需参数的值

var param1 = getParamValue('param1');

#2


2  

If you have slightly more control on your iframe sandbox, you can try postMessage API to communicate with message on events you desire to trigger.

如果您对iframe沙箱拥有更多控制权,则可以尝试使用postMessage API与您希望触发的事件上的消息进行通信。

#3


1  

you should make a serverside page like (myIframeRequest.php) php/jsp and get the parameter value in that page if it s an html page then parse the window.location.href through javascript and find the query/param

你应该创建一个像(myIframeRequest.php)php / jsp这样的服务器端页面并获取该页面中的参数值,如果它是一个html页面然后通过javascript解析window.location.href并找到查询/参数