Click to See Complete Forum and Search --> : goToUrl function not working...


Alan Klein
09-16-2003, 02:05 PM
Hello,
I'm trying to grab some information that the user enters on my HTML page and then call a "goToUrl()" function. It works when I get the data from a "prompt()", but not when I get it from an entry field on the page. The "MM_goToURL" in the following code is a function that DreamWeaver has that pops up a new window with the given .htm file.

Here's my code....

THIS ONE WORKS.....

function promptForFid() {
var returnValue;
returnValue = prompt('Enter the name of the FID to be scripted...','');
if (returnValue != null) {
if (returnValue.toUpperCase() == 'RCU' || 'MAP' || 'EBD') {
MM_goToURL('parent','Fid_' + returnValue + '.htm')
} //end if
} // end if
} // end fuction

THIS ONE DOESN'T....I'm calling this from an onClick event of a button. I know the "fid" is getting passed correctly because I had an alert() in there to see it. The new page just never opens.

function newFunction(fid) {

if (fid.toUpperCase() == 'RCU' || 'NRC' || 'EBD') {
MM_goToURL('parent','Fid_' + fid + '.htm')
} //end if
}

If anyone can see any reason why the newFunction() isn't working I would greatly appreciate it. I'm pretty new to this, and this looks like it should work. I don't know what else to try to get it to work. Thanks!!

dragle
09-16-2003, 04:16 PM
What are you passing to newFunction? If strictly a numeric, i.e., onclick="newFunction(10)" then it'll crash when it hits fid.toUpperCase, which is a string method.

Also, I strongly suspect this conditional is not doing what you think it is:

(returnValue.toUpperCase() == 'RCU' || 'MAP' || 'EBD')

The equality operator (==) receives a higher precedence than logical or's (||) so, after converting the value toUpperCase it immediately compares it to "RCU." If true, parsing stops; if not, the next portion of the condition, "MAP" is checked (by itself--i.e., not in comparison to anything) and this will always be true, since JavaScript will convert a non-empty string to a boolean value of true when needed. Or, IOW, to JavaScript, your condition looks something like this:

((returnValue.toUpperCase() == 'RCU') || ('MAP') || ('EBD'))

when what you probably wanted was something like this:

var theVar=returnValue+"";
if(/^(RCU||MAP||EBD)$/.test(theVar.toUpperCase()))

or if you need to support older browsers, the lengthier

var theVar=returnValue+"";
if((theVar.toUpperCase() == "RCU")||
(theVar.toUpperCase() == "MAP")||
(theVar.toUpperCase() == "EBD"))

Also note that calling the toUpperCase method of a string doesn't change the string itself; it only returns a changed string. Thus if you passed "map" to your function the page name you would build would be "Fid_map.htm" and not "Fid_MAP.htm".

HTH