iFrame - pull Parent URL into Hidden form field | Community
Skip to main content
April 16, 2014
Solved

iFrame - pull Parent URL into Hidden form field

  • April 16, 2014
  • 3 replies
  • 8351 views
I've identified some javascript which allows me to pull the full URL of the parent page into a hidden form field in an iFrame.  The probelm is, this code only works on Forms 1.0.

Can anyone suggest tweaks to this code so that it will work with Forms 2.0?



<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<!-- Keep your jQuery up to date -->
 
<script>
 
  $(function(){
    var url = (window.location != window.parent.location) ? document.referrer: document.location;
    $("#LeadSource").val(url);
  })
    
 
</script>
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by
<script>
$(document).ready(function(){
setTimeout(function(){
var url = (window.location != window.parent.location) ? document.referrer: document.location; $("#LeadSource").val(url); 
},3000);
});
</script>
 
Hello,
 
Above is version that uses a 3000 millisecond delay to write the LeadSource value. 
 
This will work with the drag and drop versions of forms instead of a form embed code.
 
Paste the code in a custom html element in your marketo landing page.
 
It is imperative that the Lead Source field in the form is capitalized so that the id LeadSource reflects the code value.

3 replies

April 17, 2014
Hi Kyle,

if you are using the embed script method on the landing page which is to be iframed. You need to run the code after the form renders. 

This can be accomplished with a callback method.

Insert this code before the marketo form embed code

<script>
    function getReferrer(){
        var url = (window.location != window.parent.location) ? document.referrer: document.location;
        $("#LeadSource").val(url);
    }
</script>

Then on the Marketo forms 2.0 embed code insert the function name after the form id value.

ie

    <script src="//app-sjo.marketo.com/js/forms2/js/forms2.js"></script>
    <form id="mktoForm_XXXX"></form>
    <script>MktoForms2.loadForm("//app-sjo.marketo.com", "XXX-XXX-XXX", XXXX, getReferrer);</script>

 
I would also use a different version of jquery too

<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js'></script>

I am unsure if Marketo supports version 1.9 
April 25, 2014
Hi Haven/Kyle,

So I'm using a Marketo 2.0 form iframe on our web page and I'd like to pull the URL of the parent page into a hidden form field. 

I understood that the following code goes on the webpage:
<script>
    function getReferrer(){
        var url = (window.location != window.parent.location) ? document.referrer: document.location;
        $("#LeadSource").val(url);
    }
</script>

But I don't know where to edit the next portion of the code. I'm not sure how I can edit the code of forms 2.0. Referring to the below:
<script src="//app-sjo.marketo.com/js/forms2/js/forms2.js"></script>
    <form id="mktoForm_XXXX"></form>
    <script>MktoForms2.loadForm("//app-sjo.marketo.com", "XXX-XXX-XXX", XXXX,getReferrer);</script>

Please advise.

Thanks,
Anirudh.
 

Accepted solution
April 27, 2014
<script>
$(document).ready(function(){
setTimeout(function(){
var url = (window.location != window.parent.location) ? document.referrer: document.location; $("#LeadSource").val(url); 
},3000);
});
</script>
 
Hello,
 
Above is version that uses a 3000 millisecond delay to write the LeadSource value. 
 
This will work with the drag and drop versions of forms instead of a form embed code.
 
Paste the code in a custom html element in your marketo landing page.
 
It is imperative that the Lead Source field in the form is capitalized so that the id LeadSource reflects the code value.