PDA

View Full Version : Javascript Database


chrisgray999
10-12-2002, 11:12 AM
Hi, I have the following javascript which I have created. It reads text from a "database", but I can't get it to work. I would be greatful if you could debug it for me.

page1.htm (excerpt):

<a href="javascript.htm?Javascript">Javascript</a>
<a href="javascript.htm?asp">Asp</a>

javascript.htm:

<html>
<head>
</head>
<body>
<script language="javascript">
<!-- Hide Script from old browsers

Data = new Object();
Text = new Object();
Imge = new Object();

// Number of Keywords which are in the "database"

Data[0] = 3

Data[1] = "Javascript"
Text[1] = "View the latest javascripts here!"
Imge[1] = "javascript.gif"

Data[2] = "Perl"
Text[2] = "View Perl scripts here!"
Imge[2] = "perl.gif"

Data[3] = "CGI"
Text[3] = "Find more information about CGI here"
Imge[3] = "cgi.jpg"

Data[4] = "asp"
Text[4] = "find asp scripts here"
Imge[4] = "asp.gif"




function CheckDatabase() {

var Found = false
var Show = location.search.substring(1);


for(var i=1; i<= Data[0]; i++) {
if(Show == Data[i]) {
Found = true

document.write("<h3>"+Data[i]+"</h3>")
document.write("<center>"+Text[i]+"<br><br><img src="+Imge[i]+">")

}
}


if(!Found)

document.write("Error. Requested URL is invalid, please go back and try again")

}

// End Hiding -->

</script>
</body>
</html>


Im sure there is something really stupid that i have done wrong. Basicaly, the script should take the thing after the "?" on the url and display something according to its value.

Thanks very much for any help,

Chris :)

ConfusedOfLife
10-12-2002, 12:24 PM
Well, I'm not sure, but don't you think that this line is wrong :

var Show = location.search.substring(1);

I think you have to replace it with :

var Show = location.search.substring("?")[1];

It might work!

mordred
10-12-2002, 01:23 PM
This part is correct ConfusedOfLife, substring() takes a portion of the string starting at the index position defined as the first parameter. And location.search contains already the query plus the "?" question mark before it.

chrisgray999, you have also to call the function somewhere. It won't run on it's own. ;) So better set

CheckDatabase();

somewhere near the end of your script.

Also, to see the contents for "asp", you have to increase the value in Data[0] to 4, since you have 4 rows in your quasi-Database.

BTW:
http://petra1s.die.unige.it/jsSQL/

chrisgray999
10-16-2002, 03:32 PM
Brilliant! I knew i had done something stupid! Thanks for all your help guys.