2013-04-30 16 views
-1

我目前使用这个JavaScript弹出确认重定向:打开窗口的用户是不是在指定的域

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") 

我真的很喜欢的方式来使用这个弹出只有当用户是不是有针对性的页面上。

+0

你的意思是如果引用者是aseanlegacy.net以外的东西? – MMM 2013-04-30 10:42:04

+0

你可以在window.open之前使用条件检查window.location不包含你想要的位置,然后重定向到你的页面:) – 2013-04-30 10:43:13

回答

1

我想这是你想要做什么:

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

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

如果用户在不domains域(含document.location.hostname测试),window.open将被调用。

这是JSFiddle


根据您的要求,只在打开的窗口每个会话一次,这里被修改为包括一个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"); 
} 

这里的updated JSFiddle

+0

非常感谢你所有的答案,你的答案很好,除了一个小问题,它会继续打开弹出因为代码不在域上,而是在聊天客户端脚本端。 有没有其他的方法,谢谢你的帮助 – Ayman 2013-04-30 10:53:56

+0

@Ayman你可以存储窗口是否已经用cookie打开。 – 2013-04-30 10:56:34

+0

我真的很抱歉,但我是JavaScript的noob,如果你可能告诉我如何做到这一点。 在此先感谢 – Ayman 2013-04-30 10:57:39

1

这是很清楚你想要达到的目标,但你可以抢“引荐”使用:

document.referrer 

它会告诉你用户从哪里传来。我的依据是你的报价:

如果您是通过其他网站比的加盟aseanlegacy.net ...

我绝对不知道为什么这会事,为什么你告诉否则用户网站将无法正常工作。

如果你想获得当前位置,只需使用:

document.location.href 

返回完整的URL,或

document.location.hostname 

返回的主机名。

相关问题