www.webdeveloper.com
Results 1 to 4 of 4

Thread: Small script not working : s (Javascript)

  1. #1
    Join Date
    Jul 2010
    Posts
    7

    Small script not working : s (Javascript)

    My script is not working can someone explain to me how to fix this?
    Code:
    <html>
    	<head>
    		<title>Untitled Document</title>
    	</head>
    	<body>
    		<script language="javascript">
    			
    			Nom = new Array(3)
    			
    			Nom[0] = "Justin"
    			Nom[1] = "Michel"
    			Nom[2] = "Pascal"
    
    	
    			  function addName(tbl, name){
    			
    			    tbl = Nom;
    			    name = "";
    			
    			    name = prompt();
    			
    			    tbl_addName = tbl.push(name);
    
    			    }
    			
    			function showName(){
    			
    			  tbl_show = tbl_addName;
    			
    			  return tbl_show
    			 
    			  }
    						
    addName();
    aler(tbl_show);
    
    		
    		</script>
    	</body>
    </html>
    Thank you!

  2. #2
    Join Date
    Dec 2002
    Location
    Pleasanton, CA
    Posts
    2,132
    Always include a doctype: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    -or- <!doctype html> (...the must easier HTML5>

    <script type="text/javascript"> not <script language="javascript">
    and put your javascript in the head section unless you have a GOOD reason to put it in the body

    You have declared Nom as a global array, so you don't need to parse it into the function and since Nom is global, you don't need the showName function

    Format you code a little better, you will find it's easier to read and debug
    Code:
    <!doctype html>
    <html>
    <head><title>Untitled Document</title>
    <script type="text/javascript">
    var Nom = ["Justin","Michel","Pascal"];		// an easy way to create and populate an array
    function addName() {
      var name = prompt('Enter a name','');	// Check your documentation to see how 'prompt' is used
      Nom.push(name);
    }
    
    function showName() {
      return Nom;
    }
    						
    addName();				// Parse the array into the function
    alert(Nom);				// Spelling!!
    alert(showName());		// -or- if you must
    </script>
    </head>
    
    <body>
    </body>
    </html>

  3. #3
    Join Date
    Mar 2010
    Posts
    2,803
    maybe use this as a guide

    Code:
     
    <html>
     <head>
      <title>Untitled Document</title>
     </head>
     <body>
      <script type="text/javascript">
       
       Nom = new Array(3)
       
       Nom[0] = "Justin"
       Nom[1] = "Michel"
       Nom[2] = "Pascal"
     
         function addName()  {
                    var name1 = '';
                    name1 = window.prompt('Enter a name','');
                    Nom.push(name1);
           }
       
       function showName(){
                     var str = '';
                     for(var i in Nom) {
                         str = str + Nom[i]+"\n";
                     }
            return str;
         }
          
    addName();
    alert(showName());
      
      </script>
     </body>
    </html>

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    By mistake, DFORMS reached an interesting basic referential issue of JavaScript. I have simplified a part of his code like that:
    Code:
    var Nom = ['Justin','Michel','Pascal'];
    var tbl=Nom;
    tbl.push('Phillipe');
    alert(Nom) // alerts: "Justin,Michel,Pascal,Philipe"
    That happens because the variable tbl gets a reference value, not a primitive value. That assignment var tbl=Nom set a reference, thus from now on, all the changes made in the reference (tbl) will be reflected in the main object (Nom)

    DFORMS might have thought that on using var tbl=Nom he can create another, independent, array, but he was wrong. From this point of view, a reference of the object is that object.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles