8

I know it's a security issue. But is there any way in HTML5. Cause I seen the below code opens a window with the parents URL in tweet box

<iframe src="https://platform.twitter.com/widgets/tweet_button.html"
        style="border: 0; width:130px; height:20px;"></iframe>
user2736812
  • 1,301
  • 4
  • 12
  • 15

3 Answers3

15

Try this inside the iframe. It will alert the parent window's location URL.

alert(document.referrer);
SajithNair
  • 3,867
  • 1
  • 17
  • 23
  • 10
    After doing some `url` navigation inside the `iframe`, `document.referrer` value is not returning `parent window URL`, its returning the `current navigation` we have inside the `iframe`. Is there a way to overcome this scenario? – Dipak Nov 28 '16 at 06:19
2

Give this JavaScript a go

var referrer = document.referrer;
console.log(referrer);

Hopefully it works for your purpose. More info on document.referer here if you want to know more:

https://developer.mozilla.org/en-US/docs/Web/API/document.referrer

Pete
  • 895
  • 1
  • 6
  • 13
1

One way can be by checking if window.parent[0].top.document exists.

If you don't get an error when querying this object assume it's in the same domain, otherwise assume it's in different domains.

try {
  if ( window.parent[0].top.document )
    // Chrome & Firefox (same domain)
    var origins = location.ancestorOrigins||{};

} catch(err) {
  // Firefox (different domains CORS policy)
  var origins = {"0":"*","length":1};
};
console.log(origins);
if ( !origins.length || origins[ origins.length -1 ] == location.origin ) {
  // same domain

} else {
  // different domains
  var message = "Hellow Word!!";
   window.parent.postMessage( message, origins[ origins.length -1 ] );
};
fwBasic
  • 154
  • 9