Click to See Complete Forum and Search --> : Syntax Error In While Statement
code?
August 19th, 2008, 06:57 PM
Okay, I keep getting a syntax error in my firebug add-on, and it cannot figure out what is causing the problem, but it gives me
function udf_ToggleUserStyle(box, id){if...ile (var i < newCookie.length && found =
The whole while statement is
var i = 0
while (i < newCookie.length && found == false)
{
if (id == newCookie[i]) { found = true; }
i++;
}
If you need the whole function, I can get it too, It's not long.
PeejAvery
August 19th, 2008, 07:17 PM
That isn't enough to go by. Where was newCookie defined? Why are you using a while statement instead of for? Also, instead of found == false, why don't you use !found?
code?
August 19th, 2008, 07:21 PM
That isn't enough to go by. Where was newCookie defined? Why are you using a while statement instead of for? Also, instead of found == false, why don't you use !found?
lol is that better, cleaner, or faster? never knew that.
This is a script that I ported (which i rarely do), but unfortunately this is what happens, I can usually solve it on my own.
{
if (box.checked)
{
var oldCookie = String(getCookie('scriptBlockList'));
if (oldCookie == 'null') { oldCookie = ''; }
var newCookie = oldCookie.split(',');
var found = false;
i = 0;
while (var i < newCookie.length && found == false)
{
if (id == newCookie[i]) { found = true; }
i++;
}
if (!found)
{
newCookie.push(id);
var exdate = new Date();
exdate.setDate(exdate.getDate() + 100);
setCookieWithExpires('scriptBlockList', newCookie, exdate);
}
}
else
{
var oldCookie = String(getCookie('scriptBlockList'));
if (oldCookie == 'null') { oldCookie = ''; }
var newCookie = oldCookie.split(',');
found = false;
i = 0;
while (i < newCookie.length && found == false)
{
if (id == newCookie[i]) { found = true; }
i++;
}
arrStart = newCookie.slice(0,i-1);
arrFin = newCookie.slice(i,newCookie.length);
newCookie = arrStart.concat(arrFin);
var exdate = new Date();
exdate.setDate(exdate.getDate() + 100);
setCookieWithExpires('scriptBlockList', newCookie, exdate);
}
}
javajawa
August 20th, 2008, 03:02 AM
First, here's a considerably neater version of that code. I'll see what debugging I can do soon, although knowing what id is (I'm guessing some number), what the cookie normally looks like (guessing the result of the !box.checked segment). I'm happy with box.checked...
function ??? {
var found = false; var i = 0;
var oldCookie = String(getCookie('scriptBlockList'));
if (oldCookie == 'null') {
oldCookie = '';
};
var newCookie = oldCookie.split(',');
for (i = 0; i < newCookie.length; i++) {
alert(newCookie[i])
if (id == newCookie[i]) {
found = true;
i = newCookie.length;
};
};
var exdate = new Date();
exdate.setDate(exdate.getDate() + 100);
if (box.checked) {
if (!found) {
newCookie.push(id);
setCookieWithExpires('scriptBlockList', newCookie, exdate);
};
} else {
arrStart = newCookie.slice(0,i-1);
arrFin = newCookie.slice(i,newCookie.length);
newCookie = arrStart.concat(arrFin);
setCookieWithExpires('scriptBlockList', newCookie, exdate);
};
};
code?
August 20th, 2008, 03:19 AM
I think the id is a user name
javajawa
August 20th, 2008, 03:22 AM
Ok. there are no syntax errors I can find in the code of my last post. However, I don't have any of your cookie functions or data.
The attached file is the testing bed I used.
code?
August 20th, 2008, 11:32 AM
Okay, your code works perfectly, i forgot to add that box and id were function arguments.
Anyways, now I face a slightly small problem. I made it really simple and divided it up so i could see what the problem is, and it seems to be the single quote.
(took out some attributes for simplicity)
var userID = "bob99";
var temp2 = "'" + userID + "'"; //alerts 'bob99' with alert()
var temp = "onchange='toggleUserStyle(this, " + temp2 + ");'";
It ends up giving me trouble, and looking like this...
<input );="" a10m1c="" onchange="toggleUserStyle(this, "/>
I want it like this... example username
<input onchange="togglerUserStyle(this, 'bob99');" />
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.