SOLVED

Re: Capture parent page of form in an iframe

Go to solution
John_Wolf1
Level 3

Capture parent page of form in an iframe

I am displaying a Marketo form on a non-marketo landing page via an iframe. Is there a way to capture the url of the parent page?  
Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
Dan_Stevens_
Level 10 - Champion Alumni

Re: Capture parent page of form in an iframe

This should work for you:

parentURL = (window.location != window.parent.location) ? document.referrer: document.location;

View solution in original post

8 REPLIES 8
Pierce_Ujjainw3
Level 9

Re: Capture parent page of form in an iframe

There is, but you need custom code in order to accomplish it.
Dan_Stevens_
Level 10 - Champion Alumni

Re: Capture parent page of form in an iframe

This should work for you:

parentURL = (window.location != window.parent.location) ? document.referrer: document.location;
Jim_DiModica
Level 3

Re: Capture parent page of form in an iframe

Dan -

I think I need something similar. My form is loaded into a page on our web site via an iframe. I need it to capture the referrer (ref) value from the URL:

i.e. xxx.com/static/400/?ref=DM

The form has a hidden field for the referrer. It works HERE in a non-iframe situation.

Our new reg. sign-up calls for an iframe delivery. Can you tell me what script is required in the iframe page that has the form?

Thanks!

Dan_Stevens_
Level 10 - Champion Alumni

Re: Capture parent page of form in an iframe

Jim - does the script below not work for you?  Our site no longer uses iframed marketo forms - we use embedded forms now.  The code I posted below is what we used.

Jim_DiModica
Level 3

Re: Capture parent page of form in an iframe

Dan, thanks for your reply. Re: the code posted on 11/8: When I tried it a couple of days ago there were errors so I re-posted the question. Since your reply I've re-attempted and haven't encountered errors so I'll proceed with the rest of the testing and (hopefully) site deployment. I appreciate your checking-in. Thanks again! - Jim

John_Wolf1
Level 3

Re: Capture parent page of form in an iframe

Thank you both!
Anonymous
Not applicable

Re: Capture parent page of form in an iframe

Where do you insert the script code and how do you capture the value of the parentURL variable?
Dan_Stevens_
Level 10 - Champion Alumni

Re: Capture parent page of form in an iframe

The script code is placed on the Marketo landing page (as a HTML element) - which is then reference on the non-Marketo landing page within the iFrame.  The value of parentURL is  (window.location != window.parent.location) ? document.referrer: document.location;

This is the script that we use to grab the referral URL of the page that drove someone to this this page (NOTE: "ref" is the querystring value of the page URL that has the iframe within it (e.g., www.domain.com/contact-us.aspx?ref=[some-URL]:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<!-- Keep your jQuery up to date -->

<script>
$(document).ready(function(){
var parentURL = (window.location != window.parent.location) ? document.referrer: document.location;

function getUrlVars(url) {
    var vars = [],
        hash;
    var hashes = url.slice(url.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
  }

var ref = getUrlVars(parentURL)["ref"]; //set querystring variable here
MktoForms2.whenReady(function(form){
form.setValues({"Referral_URL":ref});
});
});
</script>