JavaScript 浏览器代理重定向

发布于 2024-12-28 11:08:22 字数 351 浏览 5 评论 0原文

我的网页标题上有一小段代码,用于将用户重定向到不同的页面(如果他们使用 iPhone):

<script type="text/javascript">
if ((navigator.userAgent.indexOf('iPhone') != -1) ||  
(navigator.userAgent.indexOf('iPod') != -1)) {  
document.location = "iPhone.aspx"; }
</script>

是否有一种简单的方法来“反转”此代码,以便任何人从任何其他页面登陆 iPhone 页面浏览器会被重定向回主页吗?

非常感谢。

I've got this little bit of code on my webpage header to redirect the user to a different page if they are using an iPhone:

<script type="text/javascript">
if ((navigator.userAgent.indexOf('iPhone') != -1) ||  
(navigator.userAgent.indexOf('iPod') != -1)) {  
document.location = "iPhone.aspx"; }
</script>

Is there an easy way to 'reverse' this code, so anyone landing on the iPhone page from ANY other browser will be redirected back to the home page?

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

很酷不放纵 2025-01-04 11:08:23

简单的解决方案。用这个替换你现有的。

if(!/(iphone|ipod)/i.test(navigator.userAgent)) {
   window.location = "Desktop.aspx";
}

已更新,因为它仅适用于 iPhone 页面。

Simple solution. Replace your existing one with this.

if(!/(iphone|ipod)/i.test(navigator.userAgent)) {
   window.location = "Desktop.aspx";
}

Updated since its only for iPhone page.

花落人断肠 2025-01-04 11:08:23

要扭转?将 != -1 替换为 == -1

编辑:

假设您的意思是这样的:

if ((navigator.userAgent.indexOf('iPhone') != -1) ||  
(navigator.userAgent.indexOf('iPod') != -1)) {  
document.location = "iPhone.aspx"; } else {
document.location = "notiPhone.aspx";
}

To reverse? Replace != -1 with == -1

Edit:

Suppose you mean something like this:

if ((navigator.userAgent.indexOf('iPhone') != -1) ||  
(navigator.userAgent.indexOf('iPod') != -1)) {  
document.location = "iPhone.aspx"; } else {
document.location = "notiPhone.aspx";
}
薄情伤 2025-01-04 11:08:23

(navigator.userAgent.indexOf('iPhone') == -1 表示 userAgent 中没有这样的字符串“iPhone”。因此,当您从 != 更改为==,您还需要将||更改为&&

if ((navigator.userAgent.indexOf('iPhone') == -1) 
   && (navigator.userAgent.indexOf('iPod') == -1)) {  
    document.location = 'default.aspx';
}

(navigator.userAgent.indexOf('iPhone') == -1 means there is no such string "iPhone" in the userAgent. So when you change from != to ==, you need to change || to && as well.

if ((navigator.userAgent.indexOf('iPhone') == -1) 
   && (navigator.userAgent.indexOf('iPod') == -1)) {  
    document.location = 'default.aspx';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文