Javascript optimization

1.  Best practice to optimize for loop in Javascript

The .length property of an array is checked at each enumeration if it's constructed asfor(var i=0; i<array.length; i++). For micro-optimisation, it's efficient to store the length of an array in a temporary variable

for(var i = 0, var len = array.length; i < len; i++) {  
More @

Use Session variables without cookies

More @ :

2.  Send data from one page to another page without server scope.

Using the cookies.js library from Recipe 1.9, you can use the onunload event handler of one page to store from one to twenty name/value pairs on the user's machine. The following example captures a text input field's value and saves it to a cookie that stays on the visitor's computer for 180 days:

<script language="JavaScript" type="text/javascript" src="cookies.js"></script>
<script language="JavaScript" type="text/javascript" >
function saveData( ) {
    var data = document.forms[0].userName.value;
    var expDate = getExpDate(180, 0, 0);
    setCookie("userName", data, expDate); 
<body onunload="saveData( )">
In the second document, an onload event handler retrieves the cookie data and assigns the value to a text input field with the same name located on the second page:

<script language="JavaScript" type="text/javascript" src="cookies.js"></script>
<script language="JavaScript" type="text/javascript" >
function readData( ) {
    var data = getCookie("userName");
    document.forms[0].userName.value = data; 
<body onload="readData( )">
More @ :
  3. Another way using HTML :

How to Do ?

Example :

<FORM METHOD="LINK" ACTION="jspass2.html">
Type your first name: <INPUT TYPE="text" NAME="FirstName">
Type your last name: <INPUT TYPE="text" NAME="LastName">
<INPUT TYPE="submit" VALUE="Click and See">

     Look first at the main FORM flag. The METHOD is set to LINK and the ACTION simply points toward another page. Nowhere in there does it say to send the user information anywhere. When you set up the form in this manner, what the user inputs is carried along in the URL.
     The information is separated from the actual address by a question mark so it doesn't harm the movement to another page. This little quirk of the browser allows us to go to a totally new page and carry the user's input right along with us in the URL.

How to retrieve ?

<FORM NAME="joe">
<INPUT TYPE="hidden" NAME="burns">

<SCRIPT LANGUAGE="javascript">

var locate = window.location
document.joe.burns.value = locate

var text = document.joe.burns.value

function delineate(str)
theleft = str.indexOf("=") + 1;
theright = str.lastIndexOf("&");
return(str.substring(theleft, theright));
document.write("First Name is " +delineate(text));


More @ :

4. persistent storage Library :

supports persistent client-side storage through the following backends:
  • flash: Flash 8 persistent storage.
  • gears: Google Gears-based persistent storage.
  • localstorage: HTML5 draft storage.
  • whatwg_db: HTML5 draft database storage.
  • globalstorage: HTML5 draft storage (old spec).
  • ie: Internet Explorer userdata behaviors.
  • cookie: Cookie-based persistent storage.
More @ :