SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast Incubator's Avatar
    Join Date
    Apr 2002
    Location
    Good old Belgium
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    transferring variables

    Im having difficulties transferring a variable or arry-ay,object whatever from a <script> section in the header to a <script> section in the body.
    for example:
    Code:
     
    <html>
    <head>
    <script language="JavaScript">
       var x="a";
    </script>
    </head>
    <body>
    <script langauge="JavaScript">
      document.write(a);
    </script>
    since this doesnt work I tried passing on the variable like this:
    Code:
    <head>
    <script language="JavaScript">
    	var x="a";
    	function returnVal() {
    	   return x;
    	}
    </script>
    </head>
    <body>
    <script language="JavaScript">
       y=returnVal();
       document.write(y);
    </script>
    </body>
    but that doesnt work either
    Has anyone a way around this?
    (except an externel .js file because the ultimate purpouse for this question involves a bit more complex things than passing on simple variables, certain functions needed to be done on them and they are only allowed to be done once, so thats why an external .js file isnt good, because if I would implement it both in the header and body, they would be executed twice)
    "Press any key to continue. Press any other key to exit."

  2. #2
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <html> <head> <script language="JavaScript"> var x="a"; </script> </head> <body> <script langauge="JavaScript"> document.write(a); </script>


    First of all, this didn't work because there is no variable created called "a", and if you want to print the letter "a" on your web page, you need to surround it in quotes inside the write() method.

    Instead of using the a, insert the x in there and it will display the letter a on your page.

  3. #3
    SitePoint Enthusiast Incubator's Avatar
    Join Date
    Apr 2002
    Location
    Good old Belgium
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm, sorry for that typo, must be getting late
    the reason I asked was because I have a problem with a certain script, and I thought it wxas the variable passing, but since i mistyped i thought that was the culprit.

    Code:
    <html>
    <head>
    <script language="JavaScript">
    var xvraag = new Array(10);
    var reeks = new Array(10);
       xvraag[0]= "Test";	 
       xvraag[1]= "Bits & bytes";
    		 xvraag[2]= "= Microsoft Word =";
    		 xvraag[3]= "7/2 = 3,5";
    		 xvraag[4]= "5  is meer dan 2 $";
    		 xvraag[5]= "@ is de apestaart";
    		 xvraag[6]= "# is het hekje";
    		 xvraag[7]= "11 september 2001";
    		 xvraag[8]= "Is 6x7 gelijk aan 42?";
    		 xvraag[9]= "Alt 126 is ~";
       
    function NieuweReeks() {
       var i=0;
       var bestaat;
       var x;
       var intRandom=0;
       bestaat = false;
       while (i<10) {
    	bestaat=false;
    	intRandom = Math.floor(Math.random()*10)+1;
    	for (x=0; x<10; x++)
    	 if (intRandom==reeks[x])
    	  bestaat=true;
    	if (bestaat==false) {
    	 reeks[i]=intRandom;
    	 i++;
    	} 
       }
    }
    </script>
    </head>
    <body onLoad="NieuweReeks()">
    <script language="JavaScript">
    for (var y=0; y<10; y++)
     document.write(reeks[y]);
    </script>
    </body>
    </html>
    dont mind the dutch names.
    the only output I get here is 10 times "undefined"
    Now, I find this strange because this module to randomly generate 10 unique numbers was something I grabbed right out of my course.
    so one might presume it works.
    "Press any key to continue. Press any other key to exit."

  4. #4
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What the hell are you trying to accomplish with this script? It makes almost no sense whatsoever...

  5. #5
    SitePoint Enthusiast Incubator's Avatar
    Join Date
    Apr 2002
    Location
    Good old Belgium
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the objective is to generate an array with 10 random numbers, and then print
    an element of xvraag using that random number (stored in reeks) as index.
    but i tested that array of random numbers but it is full of undefined elements
    "Press any key to continue. Press any other key to exit."

  6. #6
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I noticed that your reeks array didn't have defined values whatsoever, but wasn't sure if you were doing that on purpose or not...

    But why don't you do something like this?

    Code:
    <Script language="JavaScript">
    function RandomStuff()
    {
    var x = Math.random();
    if ((x >= .01) && (x <= .250))
    {
    document.write(arrayName[index]);
    }
    else if ((x >= .251) && (x <= .500))
    {
    document.write(arrayName[index]);
    }
    else if ((x >= .501) && (x <= .750))
    {
    document.write(arrayName[index]);
    }
    else if ((x <= .751) && (x > .999))
    {
    document.write(arrayName[index]);
    }
    else
    {
    document.write(arrayName[index]);
    }
    </script>


Bookmarks

Posting Permissions

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