如何从父url或父iframe中获取变量? [英] How to get variable from parent url or parent iframe?

查看:116
本文介绍了如何从父url或父iframe中获取变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从父窗口url获取变量到子iframe或从父iframe获取变量到子iframe?

我有index.html文件,其中包含iframe谁有形式的形式发送数据checker.php比这个文件转发用户example.php变量即 http://www.example.com/sample.php?variable=variable example.php的iframe和iframe的

解决方案正如Eugen Rieck试图发现的那样,内容只能通过内联框架在来自同一个域名的情况下进行通信,否则您会遇到同源策略



要与iframe中的文档进行通信,您可以使用

  var frame = window.document.getElementById('yourFrame'); 
var doc = frame.contentWindow.document;

然后,您可以与iframe中文档的元素进行通信。

我将包含您试图在example.php中抓取为隐藏文本输入的变量。你可以通过运行一个查询字符串来完成这个工作:

  var queryString = new Object; 
var qstr = window.location.search.substring(1);
var params = qstr.split('&')
for(var 1 = 0; i< params.length; i ++){
var pair = params [i] .split '=');
queryString [pair [0]] = pair [1];

var variable = queryString ['variable'] //找到变量
的值document.write(< input style ='visibility:hidden'id ='variable'type = 'text'value ='+ variable +'>< / input>);

在iframed文档中,通过使用以下代码将其拉回到父级文档中:

  var variable = doc.getElementById('variable')。value; 



和bob的舅舅。

让我知道这是否有帮助....



----编辑----



或者,如果你要在父页面的函数中写这个,你可以使用它来从子页面调用它:

 函数callParentFunction(){

parent.getVariable();
返回false;






你可以运行一个onload来调用这个或者某个东西
p>

How to get the variable from parent window url to child iframe or get the variable from parent iframe to child iframe?

I have index.html file which have iframe who have the form the form send the data to checker.php than this files forward the user to example.php with variable i.e., http://www.example.com/sample.php?variable=variable the example.php have iframe and the iframe have iframe called authenticate.php .

解决方案

As Eugen Rieck is trying to find out, content can only communicate through iframes when they originate from the same domain, otherwise you will run into the same origin policy.

To communicate with a document in an iframe you can use

var frame=window.document.getElementById('yourFrame');
var doc=frame.contentWindow.document;

You can then communicate with the elements of the document in the iframe.

I would include the variable that you are trying to "grab" in example.php as a hidden text input. You can do this by running a querystring:

var queryString = new Object;
var qstr = window.location.search.substring(1);
var params = qstr.split('&')
for (var 1=0; i<params.length; i++) {
var pair = params[i].split('=');
queryString[pair[0]]=pair[1];

var variable = queryString['variable'] //find the value of variable
document.write("<input style='visibility:hidden' id='variable' type='text' value='"+ variable +"'></input>");

Once that is in the iframed document, pull it back out in the parent by using:

var variable = doc.getElementById('variable').value;

and bob's your uncle.

Let me know if this helps....

----EDIT----

Alternatively, if you were to write this in a function in the parent page, to invoke it from the child page you could use:

function callParentFunction() {

            parent.getVariable();
            return false;

}

You could run an onload to call this or something

这篇关于如何从父url或父iframe中获取变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆