GuestofHonor
GuestofHonor

Reputation: 133

How to do a page redirect using javascript?

I am currently using this code which is working great except for iframes :

var domains = ["domain1.com", "domain2.com", "domain3.com"];

if (domains.indexOf(document.location.hostname) == -1 && document.cookie.indexOf('redirected=1') == -1) {
    document.cookie = "redirected=1";
    window.location.replace('http://domain1.com');
}
alert("Join our main website ==> http://domain1.com");

but when my website is iframed in other sites the code doesn't work, and if i use window.location.open or window.location.href it will be blocked by popup blockers.

is there another way to do a redirect or a page open without being stopped by popup blockers or just a redirect that can redirect the whole page that has the iframe to another page ?

Thanks in advance.

-tool

Upvotes: 0

Views: 927

Answers (2)

user2487119
user2487119

Reputation:

Try an approach with creation of meta tag:

<meta http-equiv="refresh" content="5; url=http://example.com/;">

Or in case domain in urls of your iframe it is possible to access the parent document and hence update the iframe's src.

Upvotes: 0

LJᛃ
LJᛃ

Reputation: 8123

You can use the "top" property of window to access the parent window, like:

window.top.location="http://domain1.com";

Upvotes: 1

Related Questions