Help me to debug this :

Status
Not open for further replies.

mssssee2

Graphic Designer
Messages
506
Location
Greece
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.



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")
}
}
 
I need to learn to read...I wrote the below text before realizing how simple your question was! So, to firstly directly answer
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. :)
 
Status
Not open for further replies.
Back
Top Bottom