If 语句不能与 And (&&) 运算符一起使用
我很难写出一个看似简单的 if 语句!我需要它说如果 mod 不等于 a、b 或 c - 则执行此操作。这是我正在尝试但没有成功的方法:
var mod = CURRENT_MODULE_ID;
if (mod != "5827289" && mod != "5195103" && mod != "5181422") {
doSomething();
}
当我在编辑器中输入此内容时,它说存在错误,特别是“实体名称必须立即跟在‘&’之后在实体参考中。” ..并且当我去测试时不起作用。
任何帮助表示赞赏!
更新: 网址:esber.squarespace.com
完整脚本:
<script type="text/javascript" src="/storage/scripts/sessvars.js"></script>
<script type="text/javascript">
<![CDATA[
onload=function(){
sessvars.browserConfirmation?'none':'';
sessvars.ageConfirmation?'none':'';
};
var mod = Squarespace.Constants.CURRENT_MODULE_ID;
if (mod != "5827289" && mod != "5195103" && mod != "5181422") {
if(sessvars.ageConfirmation != "yes"){
window.location = "/verify/";
};
};
]]>
</script>
我希望网站中的每个页面在页面加载时自动重定向到验证页面,除非它是验证页面(/verify)、“您尚未验证”页面( /not-verified),或登录页面(/login)——除非用户已经通过设置 sessvars 进行了验证,否则他们可以继续访问主页。
为了测试这一点,我访问 esber.squarespace.com 并单击右侧的一个菜单项(当我完成该页面时,该菜单最终将被隐藏)——当我尝试进入另一个页面而不验证我的页面时首先我应该被重定向回 /verify 页面,但这并没有发生。
如果我将脚本修改为:
<script type="text/javascript" src="/storage/scripts/sessvars.js"></script>
<script type="text/javascript">
onload=function(){
sessvars.browserConfirmation?'none':'';
sessvars.ageConfirmation?'none':'';
};
var mod = Squarespace.Constants.CURRENT_MODULE_ID;
if (mod != "5827289") {
if(sessvars.ageConfirmation != "yes"){
window.location = "/verify/";
};
};
</script>
那么它就可以正常工作(?)
I'm having a hard time writing up what seems should be a simple if statement! I need it to say if mod does not equal a, b, or c - then do this. Here is what I was trying but have been unsuccessful:
var mod = CURRENT_MODULE_ID;
if (mod != "5827289" && mod != "5195103" && mod != "5181422") {
doSomething();
}
When I type this into my editor it says there is an error, specifically that "The entity name must immediately follow the '&' in the entity reference."
.. and is not working when I go to test.
Any help is appreciated!!
UPDATE:
The url: esber.squarespace.com
The full script:
<script type="text/javascript" src="/storage/scripts/sessvars.js"></script>
<script type="text/javascript">
<![CDATA[
onload=function(){
sessvars.browserConfirmation?'none':'';
sessvars.ageConfirmation?'none':'';
};
var mod = Squarespace.Constants.CURRENT_MODULE_ID;
if (mod != "5827289" && mod != "5195103" && mod != "5181422") {
if(sessvars.ageConfirmation != "yes"){
window.location = "/verify/";
};
};
]]>
</script>
I want every page in the site to automatically redirect on page load to the verify page, unless it is the verify page (/verify), the "You are not verified" page (/not-verified), or the login page (/login) -- unless the user already verified by setting the sessvars, then they can continue on to the homepage.
To test this I go to esber.squarespace.com and click on one the menu items at the right (this menu would eventually be hidden when I'm done with the page) -- when i try to go to another page without veriying my age first i should be redirected back to the /verify page but that isnt happening.
If i revise the script to:
<script type="text/javascript" src="/storage/scripts/sessvars.js"></script>
<script type="text/javascript">
onload=function(){
sessvars.browserConfirmation?'none':'';
sessvars.ageConfirmation?'none':'';
};
var mod = Squarespace.Constants.CURRENT_MODULE_ID;
if (mod != "5827289") {
if(sessvars.ageConfirmation != "yes"){
window.location = "/verify/";
};
};
</script>
then it works fine(?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
试试这个:
如果这不起作用,请将代码留在那里一会儿,以便我们可以直接在您的网站上调试它
Try this:
If this doesn't work, just leave the code there for a bit, so that we can debug it directly on your website
将脚本包装在 CDATA 部分中。
Wrap your script in a CDATA section.
我尝试了与你的完全相同的代码,它工作正常:
它做了“doSomething”。当值更改为 5195103 时,没有任何反应,这是正确的
除了编辑器之外,运行它时的脚本错误是什么以及您使用的浏览器是什么?我怀疑这可能是其他地方的错误或者可能与 CURRENT_MODULE_ID 相关?
I tried the EXACT same code as yours and it works fine:
It did 'doSomething'. When value is changed to 5195103, nothing happens which is correct
The editor aside, what's the script error when you run it and what's the browser you used? I suspect it could be an error elsewhere or perhaps related to
CURRENT_MODULE_ID
?您是否将此 javascript 嵌入到 xml 文档中?
听起来 xml 文档格式不正确,可能是因为 &应该转义为 &
javascript本身看起来也很好我
尝试:
你会发现这样是否需要转义javasciprt
编辑回应评论:
尝试以下操作:
Are you embedding this javascript in an xml document?
It sounds like the xml document is not well formed, perhaps because the & should be escaped as &
The javascript by itself looks fine too me
Try:
You'll find out that way whether the javasciprt needs to be escaped
Edit in response to comment:
Try the following:
听起来您的编辑器只是认为您正在处理 XML 文档。您是否尝试过在浏览器中实际运行它?如果是这样,浏览器也会报错吗?
It sounds like your editor just thinks you're working with an XML document. Have you tried actually running this in a browser? If so, does the browser also give an error?
您是否尝试将 ID 作为字符串或值进行比较?你尝试过不加引号吗?
或者另一种方法是使用 match
Are you trying to compare the ID as a string or value? Did you try it without quotes?
or another method would be to use match
我在 XSL 文件的脚本部分中收到此错误。
我在我的脚本中调整了上述答案并且它起作用了。
注意下面代码段中的CDATA部分
I got this error within a script section in an XSL file.
I adapted the above answer within my script and it worked.
Note the CDATA section in the code segment below
只需使用 != 进行比较,而不是 == 然后 &&将工作
if(val != "" && val != "") {
console.log("filled");
}else
{console.log("空"); }
just use != in comparison instead of == then && will work
if(val != "" && val != "") {
console.log("filled");
}else
{console.log("empty"); }