Question

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(?)

Was it helpful?

Solution

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

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top