为什么这个带有OR操作的If语句不能在JavaScript中工作?

Why this If statement with OR operation doesn't work in JavaScript?

本文关键字:不能 语句 JavaScript 工作 If 操作 OR 为什么      更新时间:2023-09-26

我用这段代码将用户重定向到移动UI。首先,它会检查它是否是一个移动设备,其次,它会检查是否有一个cookie,第三是检查URL,如果不包含这些词("disform,NewForm,EditForm")。但就是不

if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
        if (document.cookie.indexOf("DesktopView=") < 0) {
            if (document.location.href.indexOf('NewForm') === -1 || document.location.href.indexOf('DispForm') === -1 || document.location.href.indexOf('EditForm') === -1) {
                document.location = "/_layouts/Mobile/index.aspx";
            }                                
        }
    }
谁能告诉我为什么?

您正在检查URL是否"不包含NewForm"或"不包含disform "等

你可能是指&&

检查是否存在 cookie:

if (document.cookie.indexOf("DesktopView=") < 0) {...}

如果你想检查是否有cookie,它应该是:

if (document.cookie.indexOf("DesktopView=") >= 0) {...}