Forum Moderators: open

Message Too Old, No Replies

testing a boolean constant

using confirm result as a condition

         

nyteshade

10:24 pm on Jun 7, 2011 (gmt 0)

10+ Year Member



I am trying to display results using confirm pop up box. My problem is that I cannot seem to get my while statement to fire when using the confirm result placed in a varible:


function load(){

var j = 0;
var l = document.links.length;
userInput = true;

while((userInput == true) && (j < l))
{
userInput = confirm("This prompt will continue to list all " + l + " links in current document.\n" +"Link: " + j + " is " + window.document.links[j].href) + "\n";

j = j + 1 ;
}
}


I've tried many variations of if, do-while, while, ffs! This current chunk just displays the 0 href and then drops out of the loop. When I remove (userInput == true) then the loop continues for the remainder of the (j < l) condition.

I want user to be able to cancel the pop up AND to understand why my condition does not work as expected. Thanks all!

astupidname

10:01 am on Jun 8, 2011 (gmt 0)

10+ Year Member



It's merely a syntax error / typo thingy, this:
window.document.links[j].href) + "\n";


should be:
window.document.links[j].href);


by adding the "\n" character on to the result of the confirm there, you were essentially creating a string, depending on the result of the confirm, it would have been either: "true\n" or "false\n" and since those are strings with length they are not exactly equal to true. In fact, add this alert in after your confirm in it's present state:
alert((userInput == true) +'\n'+ (typeof userInput));

then remove the + "\n" after your confirm

nyteshade

10:31 am on Jun 8, 2011 (gmt 0)

10+ Year Member



Dang! It's those little detailly things that jump up and bite me. Never saw that \n in any displays as I was trying to debug my error since, of course, it ain't displayed. Thanks for taking the time to detail your reply. That was a insightful use of typeof. Peace.