4

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

VUELA
  • 268
  • 1
  • 7
  • 22
  • 2
    Clarification: meaning that if 'mod' is anything other than a, b, c - then do something. – VUELA Feb 05 '10 at 03:03
  • How are you getting the mod numbers? I went through the pages in your site, and none of the pages have a CURRENT_MODULE_ID that matches the mod numbers you are testing for. Except the main url page. – Waleed Al-Balooshi Feb 05 '10 at 03:36
  • It's actually Squarespace.Constants.CURRENT_MODULE_ID; ... they should all have them in the final form of #modulePage1234567 as it comes up in the source. – VUELA Feb 05 '10 at 03:49
  • I have done something similar before and it worked fine: var mod = Squarespace.Constants.CURRENT_MODULE_ID; var imgColor = "Red"; // default if (mod == "2875590" || mod == "2875610" || mod == "2875616") { imgColor = "Green"; } – VUELA Feb 05 '10 at 03:55

8 Answers8

7

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

Waleed Al-Balooshi
  • 6,318
  • 23
  • 21
4

Wrap your script in a CDATA section.

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

// script here

]]>
</script>
Lachlan Roche
  • 25,678
  • 5
  • 79
  • 77
1

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>
Martin Booth
  • 8,485
  • 31
  • 31
  • I am using an online CMS where it just gives me a box to enter in additional content in the . The error I am getting back does look like it's considering it xml?: "Could not parse your site header code. Are you inserting invalid XML? Error: Error on line 17 of document : The entity name must immediately follow the '&' in the entity reference. Nested exception: The entity name must immediately follow the '&' in the entity reference." – VUELA Feb 05 '10 at 03:13
  • It is probably validating the text as valid xhtml in that case. You might need to include the script tags and required escaping for the javascript. I have put my suggested fix as an edit to this response – Martin Booth Feb 05 '10 at 03:17
  • I tried swapping out with && -- and the error message went away but the function still does not work. I also notice that when I go back into the editor window it reverts back to &&. – VUELA Feb 05 '10 at 03:18
  • i wrapped in the CDATA and it seems to be working better because now it is saving with no error, and i can also see the javacript in the page source .... however it's still not doing what I want it to do so there mush be another problem somewhere. It works fine when I only have one item listed in my statement: if (mod != "5827289"), the problem just comes in when I try to list more than one. – VUELA Feb 05 '10 at 03:26
  • Don't forget to add comment out the CDATA parts like this: // <![CDATA[ javascript code here // ]]> – Waleed Al-Balooshi Feb 05 '10 at 03:31
  • Well if you're really struggling to get the ampersands to work in this CMS system, you can always rewrite it as follows: if (!(mod == "5827289" || mod == "5195103" || mod == "5181422")) That should be equivalent – Martin Booth Feb 05 '10 at 03:33
  • I tried that one too and it also would not work for me, though I did not get the error and didnt have to wrap with CDATA. – VUELA Feb 05 '10 at 03:37
  • I suspect the CMS did something to the codes before rednering it out to the browser. Can you view the source in the browser and see what it turns out to be? – o.k.w Feb 05 '10 at 03:41
  • I took a look at the site and the javascript code is being inserted in the correct place. It is also valid javascript. I would suggest some simple debugging (such as alert(mod);) to verify the value of mod and to check that it really is none of the values specified in that statement. – Martin Booth Feb 05 '10 at 03:50
  • Thanks Waleed you were right! needed to comment out the CDATA part! – VUELA Feb 05 '10 at 04:29
1

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 ?

o.k.w
  • 25,490
  • 6
  • 66
  • 63
0

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?

Rex M
  • 142,167
  • 33
  • 283
  • 313
  • Hi Rex! I can save it, but when I test the page it is not working. The page is at: esber.squarespace.com I am trying to set a basic age verification form, this part of the script is supposed to redirect to the age verification page unless they are on any one of these three pages designated by the mod variable. – VUELA Feb 05 '10 at 03:07
  • Actually, yes, it looks like the CMS editor is not allowing me to save that. – VUELA Feb 05 '10 at 03:11
0

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();
}
Mottie
  • 84,355
  • 30
  • 126
  • 241
0

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>
D. Kermott
  • 1,613
  • 17
  • 24
0

just use != in comparison instead of == then && will work

if(val != "" && val != "") {

console.log("filled");

}else {console.log("empty"); }

Wasi Sharief
  • 159
  • 1
  • 5