views:

216

answers:

6

Hi all! 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(?)

A: 

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
Hi Rex! I can save it, but when I test the page it is not working. The page is at: esber.squarespace.comI 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
Actually, yes, it looks like the CMS editor is not allowing me to save that.
VUELA
+1  A: 

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
VUELA
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
VUELA
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
Don't forget to add comment out the CDATA parts like this: // <![CDATA[javascript code here// ]]>
Waleed Al-Balooshi
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
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
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
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
Thanks Waleed you were right! needed to comment out the CDATA part!
VUELA
+1  A: 

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
Have been testing the page in Mac Firefox 3.5, Safari 4
VUELA
+3  A: 

Wrap your script in a CDATA section.

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

// script here

]]>
</script>
Lachlan Roche
A: 

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();
}
fudgey
i tried without quotes but have not tried the match method.
VUELA
+3  A: 

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
This worked beautifully! Thanks so much! i needed // added!
VUELA
HTML documents don't recognize the CDATA markers, so you need to have them commented out for the HTML parser to work properly. For CSS code you would have done /*<![CDATA[*/ /*]]>*/
Waleed Al-Balooshi
makes sense - thanks so much for the tip! thought i was going nuts!!
VUELA
I have been there many a time before. Happy I could help though.
Waleed Al-Balooshi