chaonextdoor
chaonextdoor

Reputation: 5129

How to distinguish the url of the browser and the iframe?

For a specific page, what I want to do is to redirect the whole page to another one if it shows normally in the browser and keep it the same when it shows in an iframe inside another page. Basically what I'm doing now is to use a simple if clause as follows:

if (window.location.href=="http://www.XXX.com") {
            window.location = "http://www.YYY.com";
        }

But it turns out that the iframe will get redirected as well. Is there anyway to distinguish these two scenarios?

Upvotes: 0

Views: 87

Answers (1)

epascarello
epascarello

Reputation: 207511

You have a typo

  • = is set
  • == is compare

and your code

if (window.location.href="http://www.XXX.com") {
                        ^

If you are in the iframe and you are in two different domains, they can not see each others locations.

If they are in the same domain, you can use window.top.

Upvotes: 1

Related Questions