This is going to be short and simple tip. There are chances that your site can be put into iframe and you probably do not want that to happen. But javascript-digimantgrathere are not chances for your site to handle such situation, only before reading this post 😛

So how do you avoid that ? Well it is fairly simple using Javascript’s window object properties. Lemme first show you the code and then quickly explain it to you.

<script type="text/javascript">
   if(window.top!=window.self)
      window.top.location="http://www.digimantra.com"; //replace the URL by ur site's URL
</script>

The window.top property of window object return you the object of the top window from the DOM hierarchy of the page. However the window.self return you an object of the iframe in which your site is opened. The if condition matches both and if they are not equal, then it changes its location to your site, this is how you wanted 😛

Now if you want to see the actual URL, i mean if u are curious to know what the window.top and window.self actually return, then you can use the following code.

<script type="text/javascript">
    alert(window.top.location);
    alert(window.self.location);
</script>

Hope that helps
Cheers!!
Sachin Khosla

Share this post: