Capture parent page of form in an iframe | Community
Skip to main content
Level 3
September 5, 2014
Solved

Capture parent page of form in an iframe

  • September 5, 2014
  • 5 replies
  • 4246 views
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?  
Best answer by Dan_Stevens_
This should work for you:

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

5 replies

Level 7
September 5, 2014
There is, but you need custom code in order to accomplish it.
Dan_Stevens_
Dan_Stevens_Accepted solution
Level 10
September 5, 2014
This should work for you:

parentURL = (window.location != window.parent.location) ? document.referrer: document.location;
Level 3
August 18, 2015

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
August 20, 2015

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.

Level 3
September 5, 2014
Thank you both!
November 7, 2014
Where do you insert the script code and how do you capture the value of the parentURL variable?
Dan_Stevens_
Level 10
November 8, 2014
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>