简体   繁体   English

在iFrame(不同域)形式的隐藏字段中,将引荐来源网址吸引到父项

[英]Grab referrer to parent in iFrame (different domain) form hidden field

I am loading a form in an iFrame using javascript and adding the referrer into URL to pass the parameter to the iFrame (step 1) for populating a hidden field on a form (step 2). 我正在使用JavaScript在iFrame中加载表单,并将引荐来源网址添加到URL中,以将参数传递给iFrame(第1步),以在表单上填充隐藏字段(第2步)。

Step 1 works fine and yields something similar to this: 第1步工作正常,并产生类似以下内容:

http://www.parentdomain.com/parentpage/?refURL=http://www.somereferringURL.com/someRefpage/ http://www.parentdomain.com/parentpage/?refURL=http://www.somereferringURL.com/someRefpage/

Step 2 (where I am getting stuck) 第2步(我陷入困境)

 function gup( grabREF ) { grabREF = grabREF.replace(/[\\[]/,"\\\\\\[").replace(/[\\]]/,"\\\\\\]"); var regexS = "[\\\\?&]"+grabREF+"=([^&#]*)"; var regex = new RegExp( regexS ); var results = regex.exec( window.location.href ); if( results == null ) return ""; else return results[1]; } var referrer = gup( 'refURL' ); function start() { var ref = document.getElementById('my-formfield-id');{ ref.value = referrer; } onload = start; } 

This only (still) seems to be yielding the parent as the referrer ( http://www.parentdomain.com/parentpage/ ), and (in fact) seems to be appending the URL string with a closing tag. 这似乎(仍然)似乎是将父级作为引荐来源网址( http://www.parentdomain.com/parentpage/ ),并且(实际上)似乎是在URL字符串后附加了结束标记。 The form and the parent reisde in different sub-domains. 表单和父级位于不同的子域中。 I am guessing the culprit may lye here: ref.value = referrer; 我猜测罪魁祸首可能躺在这里:ref.value = Referrer;

Any ideas? 有任何想法吗?

I was unable to get this to work so I resorted to grabbing only the iframe parent as the referrer and changing the parent page (and embedding the same form in the iframe) to capture the different campaign referr URLs I needed. 我无法使其正常工作,因此我只能抓住iframe父级作为引荐来源网址,然后更改父页面(并在iframe中嵌入相同的表单)以捕获所需的不同广告系列引荐来源网址。 I can still grab them in one location as I am using the same form, it just would have been nice to have just one parent (landing page) and pass along referrer to the parent. 我仍然可以像使用相同表格一样将它们放在一个位置,只有一个父母(登录页面)并将推荐人传递给父母,这将是很好的选择。

This code will do that: 此代码将执行以下操作:

 <script type="text/javascript">
    function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    }
    var refVAL = getParameterByName('refURL');
    //document.write(refVAL);

    function SetValue()
    {
    document.getElementById('my_hidden_formfield_id').value = refVAL;
    //alert(document.getElementById('my_hidden_formfield_id').value);

}
</script>

Thanks for looking. 感谢您的光临。

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

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