How to get parent domain name in iframe with cross domain?

I want to get the parent domain or url or hostname inside javascript iframe.

I used document.referrer for this, but it only works for the first time. By this I mean that my iframe contains the form, so when the user submits the form, loading the iframe again and the referrer will become my iframe domain.

Now every time my iframes are loaded, I want the parent domain name to be created only as I create the links using this.

Example:

 $(".setUrl").each(function(){ var referrer = document.referrer; this.href=referrer+"/abc.html"; }); 

But this only works for the first time for the reason I mentioned above. So can someone help me overcome this?

Ask me if more clarity is required.

+7
source share
2 answers

You can do one for this:

  • Save your parent domain / url / hostname in some hidden variable in your form. And submit this form.
  • Do not set / save / use the value of documentReferrer from the current URL. When you need, use a hidden variable.

You can use any logic ... for example:

 var refval=$("#documentReferrer").val(); if(refval=="") { $("#documentReferrer").val(referrer); //So, for the future, this will not execute... } 
+1
source

When you have the URL of the parent domain inside the iFrame (from the .referrer document), you can save that URL in localStorage and then return it from localStorage after submitting the form.

Something like:

 var referringURL = document.referrer; //store the url in localStorage localStorage['referrer'] = referringURL; 

Now after submitting the form:

 var originalReferrer = localStorage['referrer']; //you have the original referrer back 
+4
source

All Articles