0

I have no control over anything apart from the iframe.

my form sits in the iframe and I want to grab a variable that comes from the parent link.

Not sure if its poss as the parent url is a different domain.

I cant give live urls, but for example:

www.theredomain.com?ref=variable is the parent url

I need to grab the variable of 'ref' from my iframe which is on a different domain. I have tried the below but it didnt seem to do anything.

<script language="javascript">
function getUrlVars() {
var vars = {};
var parts = window.parent.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
    vars[key] = value;
});
return vars;
}
var first = getUrlVars()["ref"];
alert(first);
</script>
4

3 回答 3

1

如果它位于另一个域中,则您无法从 iframe 访问父级。

于 2012-11-12T11:33:07.633 回答
0

感谢您的输入 - 终于解决了。我使用以下函数从引荐来源网址中分离出变量

    <script>  
      function getUrlVars() {
    var vars = {};
    var parts = document.referrer.replace(/[?&]+([^=&]+)=([^&]*)/gi, function    (m,key,value)   {
    vars[key] = value;
      });
    return vars;
      }
     </script>

然后把它放到我的表格中

    <script>
        $("#submit").click(function() {
       var first = getUrlVars()["ref"];
        $(".referer").val(first);
        }); 
    <script>
于 2012-11-12T12:39:11.163 回答
0

在不在同一个域中的两个 iframe 之间发送数据的一种方法是使用postMessage

这是相当直截了当的。一方面,您实现了一些 js 作为事件侦听器,它可以识别它来自的 url,另一方面,您使用 .postMessage 方法发送到该父 iframe。如果它适用于您,请检查浏览器支持。

于 2012-11-12T11:38:05.373 回答