I got many questions same as mine, but I want really to get browser url instead of iframe url using php or jquery or javascript, any of this. I wrote several code using both php and javascript, but they not worked for me. any suggestion please?
2 Answers
if you are on a single domain (browser and iframe url) then a simple window.top.location.href should return you the browser url.
if the domain are different you can't retrieve any information from the parent location
2 Comments
Pritom
Is there any way to know that my page is inside an iframe or not?
malko
you can check if window.top.location and window.location are different in which case you certainly are in an iframe (once again if parent and frame are from same domain, if not you won't be able to read the window.top.location in which case you also probably are in an iframe)
Something like:
<script>
var url = window.location;
alert(url);
</script>
will give you the page's url
See https://stackoverflow.com/a/115533/402706 for getting the parent's url while inside an iframe.
2 Comments
Brandon J. Boone
@Pritom, Right, it will give you the url of the page it is on(parent or iframe). See the above link for getting the iframe's parent's url.
Pritom
I can use
if (window.location==top.location) to differ iframe url and browser url, but not get browser url.
parent.location.href, PHP_SELF etc..........