最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get parent domain name in iframe with cross domain? - Stack Overflow

programmeradmin2浏览0评论

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 bee 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 overe this?

Ask me in case if more clarity required.

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 bee 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 overe this?

Ask me in case if more clarity required.

Share Improve this question edited Jul 2, 2015 at 4:07 Dylan Valade 5,6056 gold badges44 silver badges57 bronze badges asked Feb 5, 2013 at 6:42 maheshmahesh 4,77311 gold badges46 silver badges61 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

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:

  1. Save your parent domain / url / hostname in some hidden variable in your form.And submit that form.
  2. Do not set/save/use documentReferrer value from current url. When you need, use the hidden variable.

You can use any logic... e.g:

var refval=$("#documentReferrer").val();
if(refval=="")
{
     $("#documentReferrer").val(referrer); //So, for the future, this will not execute...
}
发布评论

评论列表(0)

  1. 暂无评论