Home > OS >  Can a jQuery script in an HTML page post page URL to embedded iframe form?
Can a jQuery script in an HTML page post page URL to embedded iframe form?

Time:10-24

I'm not sure this is possible, but I currently have an iframe form embedded in a page and I want to collect the page URL and post it in a hidden input in the iframe form.

I have a JavaScript to take the page URL (shown below) but is it possible to adapt to post it to a hidden input on the form in an iframe?

<script type="text/javascript">            
window.onload=function()
{
document.getElementById('zf_referrer_name').value = window.location.href;
}
</script>

Iframe script for form:

<iframe frameborder="0" style="height:500px;width:99%;border:none;" src='https://forms.zohopublic.eu/mijaspueblaaidaproperties/form/ImInterestedonthisproperty/formperma/NgNHU1DHqxk95Hy2rsPlSmVF_uiHfOc3BLwEeSxrPms'></iframe>

CodePudding user response:

To answer you question directly, yes it's possible by adding an onl oad handler to your iFrame and doing something like:

<iframe frameborder="0" id="myiframe" style="height:500px;width:99%;border:none;" src='https://forms.example.com/...' onl oad="updateReferrerField(this)"></iframe>

<script>
    var updateReferrerField = function(frame) {
      frame.contentWindow.document.getElementById("zf_referrer_name").value = window.location.href;
    }
</script>

However, I suspect you're going to run into security restrictions with CORS blocking any attempt to access the iframe's DOM.

If you have access to add javascript to the embedded form you can get the parent's url using the document referrer something like the following:

function getParentUrl() {
    if (parent !== window)
        return document.referrer;

    return null; //not inside iframe
}

To answer your question another way, you might be able to pass a parameter on the URL to zoho forms in the src attribute of your iFrame by appending something like:

?MyReferrerField='https://example.com/myform'

However you'll need to check if this is something Zoho Forms can use.

  • Related