Tuesday, March 9, 2010

Redirect a URL inside a frame to a _top page

It’s easy to redirect a URL if you don’t care whether you are from inside a frame or to its own frame using META redirection or window.location javascript redirection.
What if your HTML is inside a frame calling to redirect itself to a _top page? Tricky?

I use this:

if (document.referrer.indexOf('YourAliasName') != -1) {
   top.location.href='http://www.YourMainDomainName.com';
}

You can substitute YourAliasName with a portion of your domain alias name so that the redirection won’t go into infinite loop.

I needed this because my hosting company enclosed my domain alias inside a frame and I need to redirect the domain alias to the main domain name so that it is like a normal redirection without being redirected back inside the frame. Therefore the code above that is highlighted in red is important to move the redirected page to the _top page level.

No comments:

Post a Comment