tags:

views:

49

answers:

1

Could you tell me whats wrong with this ASP script:

I think the error is in the if statement

<script>
productID=new Array()
variaveis=location.search.replace(/\x3F/,"").replace(/\x2B/g," ").split("&")
if(variaveis!=""){
for(i=0;i<variaveis.length;i++){
nvar=variaveis[i].split("=")
productID[nvar[0]]=unescape(nvar[1])
}
}
function QueryString(variavel){
return productID[variavel]
}
document.writeln (QueryString("c"));
var flash = (QueryString("c"));
if (flash = "flash1") 
{
document.write("<b>flash1</b>");
}
else if (flash = "flash2")
{
document.write("<b>flash2</b>");
}
else
{
document.write("<b>another</b>");
}
</script>
+4  A: 

Replace

if (flash = "flash1")

with

if (flash == "flash1")

etc.

A single = is for assignment, not for testing equality. JSLint is a great tool for picking up these kinds of errors:

Error:
Problem at line 1 character 20: Use the array literal notation [].

productID=new Array()

Problem at line 1 character 22: Missing semicolon.

productID=new Array()

Problem at line 2 character 77: Missing semicolon.

variaveis=location.search.replace(/\x3F/,"").replace(/\x2B/g," ").split("&")

Problem at line 5 character 33: Missing semicolon.

nvar=variaveis[i].split("=")

Problem at line 6 character 41: Missing semicolon.

productID[nvar[0]]=unescape(nvar[1])

Problem at line 11 character 29: Missing semicolon.

return productID[variavel]

Problem at line 16 character 11: Expected a conditional expression and instead saw an assignment.

if (flash = "flash1")

Problem at line 20 character 16: Expected a conditional expression and instead saw an assignment.

else if (flash = "flash2")

Implied global: productID 1,6,11, variaveis 2,3,4,5, i 4,5, nvar 5,6, unescape 6
RedFilter
And if this error happens a lot, you may consider writing it "backwards" so that you get a parsing error like so `if("flash1"=flash)` doesn't compile
Earlz
@Earlz: this never made sense to me, because if I can remember to do this, I can remember to use `==`. /:]
RedFilter
@Orb man, I personally don't do it for exactly that reason. It's much more handy though if you are just beginning programming to make this a habit from the very start though
Earlz