-2

I have an iframe , user navigated to certain location of external site in that iframe . Now i have a button that on click tells the location of that site (using .location.href) but it alerts undefined ? My site was ideone.com . How can i get its current URL ? Any correction or alternative ?

Adam Smith
  • 35
  • 1
  • 7

2 Answers2

2

Due to same-origin policy you cannot access any information from an iframe which has its location set to a different domain then the parent.

If you look at your browser's console (F12) you will probably see an error similar to this (this one is from Chrome)

Unsafe JavaScript attempt to access frame with URL http://othersite.com/ from frame with URL http://yoursite.com. Domains, protocols and ports must match.

lostsource
  • 21,070
  • 8
  • 66
  • 88
0

I think you would need to access the window of the iframe, which according to this answer you should be able to do via:

document.getElementById('myframe').contentWindow.location.href

Get a reference to your iframe however you wish, of course, as the key is .contentWindow. However, as that link indicates, if it is another domain, then the Same Origin Policy prevents you from getting the window reference.

Community
  • 1
  • 1
ajp15243
  • 7,704
  • 1
  • 32
  • 38