I want to get the parent domain or url or hostname inside iframe javascript.
I have used document.referrer
for that, but it only works the first time. By this I mean that my iframe contains the form so when the user submits the form the iframe loads again and the referrer will become the my iframe's domain.
Now each time my iframe loads I want the parent domain name only as I am creating the links using that.
Example:
$(".setUrl").each(function(){
var referrer = document.referrer;
this.href=referrer+"/abc.html";
});
But this only works the first time because of the reason I mentioned above. So can somebody help me overcome this?
Ask me in case if more clarity required.
The moment you have the parent domain URL inside the iFrame (from the document.referrer), you can store that URL in the localStorage, and then after the form is submitted, retrieve it back from the localStorage itself.
Something like:
var referringURL = document.referrer;
//store the url in localStorage
localStorage['referrer'] = referringURL;
Now after the form is submitted:
var originalReferrer = localStorage['referrer'];
//you have the original referrer back
You can do one thing for this:
You can use any logic... e.g:
var refval=$("#documentReferrer").val();
if(refval=="")
{
$("#documentReferrer").val(referrer); //So, for the future, this will not execute...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With