I need to fix a problem with my website, and have come across two similar javascripts that are supposed to do the exact same thing to fix it:
1.
This HTML code should go in the head content area of the child pages:
<script language="javascript" type="text/javascript"> if(self==top) { top.location.href="/?url="+self.location.href; } </script>
and put this script block at the end of your BODY section after the close of the IFRAME:
<script language="javascript" type="text/javascript"> if (top.location.search.substr(0,5)=="?url=") { self.frames["ifrm"].location.href = top.location.search.substr(5); } </script>
2.
<script type="text/javascript">
if (self.location==top.location)
top.location.replace(‘http://contain.er/page-url/here‘);
</script>
What are the differences if any, and if there are differences, which one will work better in my case?
For layout reasons I have top level navigation pages (Sections) which contain an iframe. The iframe is for sub navigation (topics), and the content pages are in a third iframe below it. Needless to say links to my topics and contents from other domains do not load in their parent frames, and the layout and navigation are broken. The scripts are supposed to fix it.
If you want to see for yourself: "My site from the top"
You can "Enter" the site, and navigate to "Guitars" then to "New home planet", and all is well, but this does not work:
"Direct link to the New home planet page."
Thanks in advance for your help!
The first one compares two "window" objects, self and top, to see if the current window (self) is the topmost (top) window, i.e. it checks that the window is not being framed.
It also uses location.href to redirect the browser if there is a match.
The second one does the same thing, but it compares the window.location object of the current window and the topmost window (self.location==top.location) instead of comparing the windows directly.
The second one uses location.replace to redirect.
The difference from location.href is in history only, replace does not add a history entry, it just replaces the current URL.
Functionally, they do the same thing, if the window is not loaded in an iframe, it's redirected to the given URL.
However, I have no idea how that is supposed to fix issues with your iFrames not loading.
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