简体   繁体   中英

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

Step 1 works fine and yields something similar to this:

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

Step 2 (where I am getting stuck)

 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. The form and the parent reisde in different sub-domains. I am guessing the culprit may lye here: 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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