-1

I'm currently using this JavaScript popup confirmation redirect:

var answer = confirm("If you are joining us through site other than" +
  "website.net, .com or .info please hit OK otherwise hit cancel!");

if(answer)
    window.open('http://website.net', '_blank');
else
    alert("You need to know that it will not work for you well if you don't")

I would really like a way to use this popup only if user was not on targeted page.

GuestofHonor
  • 125
  • 4
  • 16
Ayman
  • 131
  • 1
  • 9
  • You mean if the referrer was something else than aseanlegacy.net? – MMM Apr 30 '13 at 10:42
  • you can use conditional check before window.open window.location is not contain your desired location then redirect to your page :) – Ankur Loriya Apr 30 '13 at 10:43

2 Answers2

1

It is quite unclear what you want to achieve, but you can grab the "referrer" using:

document.referrer

Which will tell you where the user came from. I base this on your quote:

If you are joining us through site other than aseanlegacy.net...

I have absolute no idea why this would matter, and why you tell the user the website will not work well otherwise.

If you want to get the current location, simply use:

document.location.href

which returns the full URL, or

document.location.hostname

which returns the hostname.

MMM
  • 7,221
  • 2
  • 24
  • 42
1

I think this is what you're trying to do:

var domains = ["aseanlegacy.net", "aseanlegacy.com", "aseanlegacy.info"];

if(domains.indexOf(document.location.hostname) == -1)
    window.open("http://aseanlegacy.net", "_blank");

If the user is at a domain not in domains (tested with document.location.hostname), window.open will be called.

Here's a JSFiddle.


Per your request to only open the window once per session, here is the code modified to include a cookie:

var domains = ["aseanlegacy.net", "aseanlegacy.com", "aseanlegacy.info"];

if(domains.indexOf(document.location.hostname) == -1 && document.cookie.indexOf("opened=1") == -1)
{
    document.cookie = "opened=1";
    window.open("http://aseanlegacy.net", "_blank");
}

Here's the updated JSFiddle.

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
  • Thanks a lot for all your answers, your answer works fine except for one small problem, it will keep opening popup infinitely since that the code is not on a domain but on chat client script side. is there any other way, thanks for all the help – Ayman Apr 30 '13 at 10:53
  • @Ayman You can store whether the window has been opened with a cookie. – Danny Beckett Apr 30 '13 at 10:56
  • I'm really sorry but i'm noob in javascript, if you may show me how to do that. thanks in advance – Ayman Apr 30 '13 at 10:57
  • thanks a lot it's working great except for one small not really important issue, is there a way to detect if popup was blocked before saving cookie ? or should i just add a note for users to disable their popup blocker before entering ? Thanks in advance – Ayman Apr 30 '13 at 11:10
  • @Ayman You can refer to this question: [Detect blocked popup in Chrome](http://stackoverflow.com/q/668286) – Danny Beckett Apr 30 '13 at 11:12
  • Thanks a lot but that can help someone who really knows about JS not an illiterate :) Thanks again – Ayman Apr 30 '13 at 11:14