Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 05-01-2007, 01:00 PM   #1 (permalink)
mssssee2's Avatar
 
Graphic Designer

Join Date: Apr 2007

Location: Thessaloniki, Greece

Posts: 507

mssssee2 is on a distinguished road

Send a message via MSN to mssssee2 Send a message via Yahoo to mssssee2
Default Help me to debug this :

It's a javascript mini script/program I made. When clicking a particular photo prompts you to enter the password to redirect you to a new page, but the function of length finds a bug when clicking cancel. Any ideas? It's the following script : (I took off the image) Also the password is contained in a variable, because i had it in js format in cgi-bin, because this way it can be read only by the browser.



Quote:
function pass() {
var vs = window.prompt("PASSWORD TO EDIT :","XXXXXX")
var ls = vs.length
if (ls >= 5) {
}
else {
alert("ERROR 1 : The password must be over 5 characters long.")
}
if (ls == 0) {
alert("ERROR 2 : NULL OBJECT")
}
var vssnull = ""
var vssnull2 = " "
var ss = "12369*"
//PASSWORD
var vssnull3 = "XXXXXX"

if (vs == null) {
alert("ERROR 3 : NO PASSWORD INSERTED")
}
if (vs == vssnull) {
alert("ERROR 3 : NO PASSWORD INSERTED")
}
if (vs == vssnull2) {
alert("ERROR 3 : NO PASSWORD INSERTED")
}
if (vs == vssnull3) {
alert("ERROR 3 : NO PASSWORD INSERTED")
}
if (vs == ss) {
window.location = "http://i-plus.enosi.org/cgi-bin/util/fm_nmp"
}
else {
alert("ERROR 4 : INVALID PASSWORD")
}
}

__________________
Mike from Greece, 16

mssssee2 is offline  
Old 05-01-2007, 02:14 PM   #2 (permalink)
 
Monster Techie

Join Date: May 2004

Location: Tucson, AZ, USA

Posts: 1,183

Vormund

Send a message via AIM to Vormund Send a message via MSN to Vormund Send a message via Yahoo to Vormund
Default Re: Help me to debug this :

I need to learn to read...I wrote the below text before realizing how simple your question was! So, to firstly directly answer
Quote:
the function of length finds a bug when clicking cancel. Any ideas?
When the user cancels, the "vs" object is null, has no properties, and therefore has a zero length. So as the example below shows, all ye' gotta do is either surround everything with

if (vs != null) {
...code...
}
or add an

if (vs == null) {
return;
}

immediately after the user is prompted.
--

Hmm, I did some reformatting and changed all if's to if-else cases, as it seems that's what you would want as opposed to a dozen errors coming up (but I may be wrong!).

I tested the code below and all cases work - length, invalid pass, valid pass, canceling etc. The thing to note with your code, you always want to terminate a line with the ';' character, even though it works without 'em, it's standard and allows commenting after.

Also, you only want to continue if there is no null object (IE: the user cancels), which is why I put the return in there.

Code:
<script type="text/javascript">
function pass() {
	var vs = window.prompt("PASSWORD TO EDIT :","XXXXXX");
	
	if (vs == null) {
		// the user cancelled
		return;
	}
	
	var ls = vs.length;

	var vssnull = "XXXXXX";
	var ss = "12369*"; //PASSWORD

	if (ls < 5) {
		alert("ERROR 1 : The password must be over 5 characters long.");
	} else if (vs == vssnull) {
		alert("ERROR 3 : NO PASSWORD INSERTED");
	} else if (vs == ss) {
		window.location = "http://i-plus.enosi.org/cgi-bin/util/fm_nmp";
	} else {
		alert("ERROR 4 : INVALID PASSWORD");
	}
}
</script>
Hope that helps.
__________________
Vormund is offline  
Old 05-02-2007, 03:13 PM   #3 (permalink)
mssssee2's Avatar
 
Graphic Designer

Join Date: Apr 2007

Location: Thessaloniki, Greece

Posts: 507

mssssee2 is on a distinguished road

Send a message via MSN to mssssee2 Send a message via Yahoo to mssssee2
Default Re: Help me to debug this :

Thanks a lot, no bug executed.
__________________
Mike from Greece, 16

mssssee2 is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On