simple question

M

Michael

Hi,

can anyone tell me why the second 'alert()' is not executed here?
The first 'alert()' pops up (so I know I am inside the 'if'), but not the
second one, and orderNum is not set....

function addbook(bookname){
var orderNum = 0;
var cookies = document.cookie;
var val = cookies.split(";");
var i = 0;
var val2;
while(val != null){
val2 = val.split("=");
if(val2[0].search(/(order)(\d)/) > -1){
alert();
orderNum = $2;
alert("orderNum = "+$2); <<<<<<<<------- this alert
is not displayed
}
i++;
}
orderNum++;
alert("new cookie is: "+"order"+orderNum+"="+bookname+";");
document.cookie = "order"+orderNum+"="+bookname+";";
return true;
}


Thanks for any help

Michael
 
I

Ian Collins

Michael said:
Hi,

can anyone tell me why the second 'alert()' is not executed here?
The first 'alert()' pops up (so I know I am inside the 'if'), but not the
second one, and orderNum is not set....

function addbook(bookname){
var orderNum = 0;
var cookies = document.cookie;
var val = cookies.split(";");
var i = 0;
var val2;
while(val != null){
val2 = val.split("=");
if(val2[0].search(/(order)(\d)/) > -1){
alert();
orderNum = $2;

$2?
 
M

Michael

Thanks for that.

Regards
Michael

Duncan Booth said:
Ian Collins said:
Michael said:
Hi,

can anyone tell me why the second 'alert()' is not executed here?
The first 'alert()' pops up (so I know I am inside the 'if'), but not
the second one, and orderNum is not set....

function addbook(bookname){
var orderNum = 0;
var cookies = document.cookie;
var val = cookies.split(";");
var i = 0;
var val2;
while(val != null){
val2 = val.split("=");
if(val2[0].search(/(order)(\d)/) > -1){
alert();
orderNum = $2;

$2?

It's a perfectly valid variable name. In this case a global variable since
the OP doesn't appear to have defined it anywhere in the posted code, and
it probably has an undefined value since it looks as though the OP has it
confused with the placeholders in a regular expression substitution.

Either the match method on the String, or the exec method on the RegExp
would work better:

var match = /(order)(\d)/.exec(val2[0]);
if (match) {
ordernum = m[2];
}

although only of course for orders 0-9.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top