0

I show in iFrame my contact form (for leads). What I need to do is to save the parent url, then to insert this url to my hidden field before the form is submitted (or on the page load). I need to know from what url the form was submitted.

I have code that works only is chrome console. but when I put this code in the parent html the code is there but not working

jQuery(".gfiframe").contents().find("#gform_submit_button_1").click(function(){
    jQuery(".gfiframe").contents().find("#input_1_2").val('test');
});

To get the parent url I use

var $location = location.host;
im_brian_d
  • 7,772
  • 2
  • 26
  • 43
need-help
  • 103
  • 1
  • 4
  • I don't think you can get the URL of the parent from the iframe: http://stackoverflow.com/questions/3420004/access-parent-url-from-iframe – jwatts1980 Sep 05 '14 at 13:46
  • 1
    @jwatts1980: the linked question/answer only applies if the iframe is on a different domain, if both are on the same domain, you _can_ get them to interact through `window.parent` or `window.top` – Elias Van Ootegem Sep 05 '14 at 13:49

1 Answers1

1

location.host is relative to the iframe. Instead, use document.referrer jsFiddle Example

When the iframe's HTTP headers are sent, the parent to the iframe is sent as the referrer, therefore using document.referrer will result in the iframe's parent URL

Update: Within your iframe, not externally, use jQuery to add the parent url to the form being submitted

$('input.parent_url').val(document.referrer);
im_brian_d
  • 7,772
  • 2
  • 26
  • 43
  • Hi sorry for delay. this did not help me because the ifame and the parent on different locations.
    now i'm trying to make this work true Cross-Domain Messaging With postMessage
    – need-help Sep 16 '14 at 07:43
  • using `document.referrer` does exactly that as the example demonstrates. http://jsfiddle.net/djeeoecq/3/ the first alert is jsfiddle's iframe, and the second is the url of the page the iframe is within – im_brian_d Sep 16 '14 at 13:03
  • You have to save the url from within the iframe, not access it from the parent – im_brian_d Sep 16 '14 at 13:04