Javascript Samples Continued

Function
function to open a window
	function open_blank()
	{
		var newwin;		//blank html window
		newwin=window.open("blank.html", "Class_info", 
			"menubar=yes, width=250, height=200");
		newwin.document.write ("ehhlo");
	}

//Example code to call user-defined function to open window
  document.write("XXD");
  open_blank();
open window and write to it
   newwin=window.open("blank.html", "Class_info", "menubar=yes, width=250, height=200");
   newwin.document.write ("hello");
Passing a form to a function
function accepting(formname)
	{
		 newwin=window.open("","junk","");
		 newwin.document.write("form is " + formname.name );
		 newwin.document.write("<br> value is " + formname.first.value);
		 newwin.document.write("<br> value is " + A.first.value);
	}
Passing by value a function

   function start(received)
   {
      document.write( "<br>received is: " + received );
      received = received + 8;
      document.write( "<br>received in function is now: " + received );
   }

   
  var parm=4;
     
  document.write ("<br><b>Main page var is " + parm+ "<b>");
  start(parm); 
  document.write ("<br><b>Main page var is now: " + parm+ "<b>");

scoping
   var x = 1;      // global variable

   function start()
   {
      var x = 5;   // variable local to function start

      document.writeln( "local x in start is " + x );

      functionA();   // functionA has local x
      functionB();   // functionB uses global variable x
      functionA();   // functionA reinitializes local x
      functionB();   // global variable x retains its value

      document.writeln( 
         "<P>local x in start is " + x + "<P>" );
   }

   function functionA()
   {
      var x = 25;  // initialized each time functionA is called

      document.writeln( "<P>local x in functionA is " + x +
                        " after entering functionA" );
      x = x+ 1;
      document.writeln( "<BR>local x in functionA is " + x +
                        " before exiting functionA<P>" );
   }

   function functionB()
   {
      document.writeln( "<P>global variable x is " + x +
                        " on entering functionB" );
      x = x*10;
      document.writeln( "<BR>global variable x is " + x +
                        " on exiting functionB<P>" );
   }
scoping and a loop
   var x;

   function output()
   {
      var x;
      for ( x = 1; x < 10; x++ ) 
         document.writeln( cube( x ) + "<BR>" );
   }
   
   function cube( y )
   {
      return y * y * y;
   }
loops and functions
	function upto (initial, last)
	{
		var i;
		var step;
		
		step = parseInt(window.prompt ("Enter the upto step", "1"));
		for (i=initial; i< last; i+=step)
			document.write ("<br>", i);
	}
	
	function downto(initial, last)
	{
		var i;
		var step;
		
		step = parseInt(window.prompt ("Enter the downto step", "-1"));
		for (i=initial; i>= last; i+=step)
			document.write ("<br>", i);
	}

 var initial,
 	 last;
	 
 initial = parseInt(window.prompt("Enter the initial value", "1"));	 
 last = parseInt(window.prompt("Enter the last value", "10"));
 upto(initial, last);
 downto(initial, last);
loop, ifs and functions
	function upto (initial, last, step)
	{
		var i;

		for (i=initial; i< last; i+=step)
			document.write ("<br>", i);
	}
	
	function downto(initial, last, step)
	{
		var i;
	
		for (i=initial; i>= last; i+=step)
			document.write ("<br>", i);
	}

 var initial,
 	 last,
	 step;

 initial = parseInt(window.prompt("Enter the first value", "1"));	 
 last = parseInt(window.prompt("Enter the last value", "10"));
 step = parseInt(window.prompt ("Enter the step. Positive or negative number (-1)", "1")); 
 if (step > 0)
    upto(initial, last, step);
 else if (step < 0)
    downto(initial, last, step);
 else
 	document.write ("<br>A step of 0 is not permitted");