the follwong is the basic java script i wrote and its not working (giving the desired output as i wanted )

<html>
<head>
<title>hello </title>
</head>
<body>

<h1 align='center'> Using Arrays </h1>

<script language = "JavaScript">

emp = new array(5)
emp[0] = rahul
emp[1] = rahul1
emp[2] = rahul2
emp[3] = rahul3
emp[4] = rahul4

document.write(emp[0]+"<br>")


document.write(emp[1]+"<br>")

document.write(emp[2]+"<br>")

document.write(emp[3]+"<br>")

</script>

</body>


</html>

i have saved this file as jscp1.html

and i thought it will print all the array values , but it displayed only the header USING ARRAY is printed , rest all is just ignored by the browser.
Is there ne way to fix it , then how..?

Recommended Answers

All 7 Replies

Member Avatar for Dukane

Try this:

<html>
<head>
<title>hello </title>
</head>
<body>
<h1 align="center">Using Arrays</h1>

<script lang="JavaScript" type="text/javascript">
	emp = new Array(5);
	emp[0] = "rahul";
	emp[1] = "rahul1";
	emp[2] = "rahul2";
	emp[3] = "rahul3";
	emp[4] = "rahul4";
	
	document.write(emp[0]+"<br>");
	document.write(emp[1]+"<br>");
	document.write(emp[2]+"<br>");
	document.write(emp[3]+"<br>");
</script>

</body>
</html>

First of all, it should be Array, not array. Then since you want to print out string literals, and not variables, the strings have to be enclosed in quotation marks. I was able to diagnose this using just the basic error console contained in Mozilla Firefox. It's very handy for fixing basic JavaScript problems.

sax, it's very basic concepts, you should before code it

Hi rahul,
you could also try this simple application:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>Free Live Help!</title>
</head>
<body>
<script type="text/javascript">
<!--
   var emp = new Array( 5 );
   for ( var i = 0; i < emp.length; i++ ) {
      document.write( "rahul" + (( i ) ? i : "" ) + "<br>" );
   } 

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

@ JavaScriptBank - your site sure brings a brief knowledge to those, who are interested on dealing in the world of JavaScript...

regards
-essential

kudos to both essential and dukane for provodong me the answer.
but i have few doubts

1.wats the difference between

<script lang="JavaScript" type="text/javascript">

and

<script language = "JavaScript">

and

<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">

2.IS there any java script editor which will be usefull of newbies for me ?

3.can database programming (MySQL connection where storing and retrieving the data is done )can be done using javascript or only PHP has to be used ?

Member Avatar for Dukane

1. Look into proper HTML coding and standards compliance, they will clue you into why we have added those tags to your code.

2. I don't know of an editor or an IDE. If I code JavaScript, I like to use Crimson Editor, which is just a basic text editor. For debugging, I use the built in JavaScript error console with Mozilla Firefox to clue me in as where there are potential problems.

3. JavaScript is client side only. To work with the server (MySQL) you need a server side language. JavaScript is not one of those. PHP is one of them, but there are also plenty of others.

yeah , but i prefer to eliminate various exceptions in the client side itself , before processing it for the server side.
If i go wrong in the server side , the it also screws up the database plus the processing of script which is a very high price to pay.

Member Avatar for Dukane

yeah , but i prefer to eliminate various exceptions in the client side itself , before processing it for the server side.
If i go wrong in the server side , the it also screws up the database plus the processing of script which is a very high price to pay.

This is true, but that still doesn't make it possible to access a MySQL database with JavaScript...

Of course, you can do data validation on a form with JavaScript, but the actual processing to and from the database needs to be done with some kind of server-side language.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.