If Statement not working with And (&&) Operator

好久不见. 提交于 2019-12-05 22:26:34

Try this:

// <![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/";
   };
};

// ]]>

If this doesn't work, just leave the code there for a bit, so that we can debug it directly on your website

Wrap your script in a CDATA section.

<script type="text/javascript">
<![CDATA[

// script here

]]>
</script>

I tried the EXACT same code as yours and it works fine:

function doSomething() {alert("doing");}
var CURRENT_MODULE_ID = 5195103000;
var mod = CURRENT_MODULE_ID;
if (mod != "5827289" && mod != "5195103" && mod != "5181422") {
   doSomething();
}

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 ?

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:

var mod = CURRENT_MODULE_ID;
if (mod != "5827289" &amp;&amp; mod != "5195103" &amp;&amp; mod != "5181422") {
   doSomething();
}

You'll find out that way whether the javasciprt needs to be escaped

Edit in response to comment:

Try the following:

<script type="text/javascript">
<![CDATA[
var mod = CURRENT_MODULE_ID;
if (mod != "5827289" && mod != "5195103" && mod != "5181422") {
   doSomething();
}
]]>
</script>

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?

Are you trying to compare the ID as a string or value? Did you try it without quotes?

var mod = CURRENT_MODULE_ID;
if (mod != 5827289 && mod != 5195103 && mod != 5181422) {
   doSomething();
}

or another method would be to use match

var mod = CURRENT_MODULE_ID;
if (!mod.match("5827289|5195103|5181422")) {
   doSomething();
}

I got this error within a script section in an XSL file.

Entity '&' not defined

I adapted the above answer within my script and it worked.

Note the CDATA section in the code segment below

<script>
  var  Quantity860=<xsl:value-of select="$QuantityOrdered_860" />;
  var  Quantity850=<xsl:value-of select="$QuantityOrdered_850" />;
  var  QuantityToReceive860=<xsl:value-of select="$QuantityLeftToReceive_860" />;

  if(parseFloat(Quantity860.textContent) !== parseFloat(Quantity850.textContent) <![CDATA[ && ]]> parseFloat(QuantityToReceive860.textContent) !== parseFloat(Quantity850.textContent))
  {
      Quantity860.style.color="#FF6347";
      Quantity850.style.color="#FF6347";
      QuantityToReceive860.style.color="#FF6347";
  }
</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!