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 @ 
http://stackoverflow.com/questions/8452317/do-loops-check-the-array-length-every-time-when-comparing-i-against-array-length

Use Session variables without cookies

More @ :
http://www.thomasfrank.se/sessionvars.html

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); 
}
</script>
...
<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; 
}
</script>
...
<body onload="readData( )">
 
More @ : 
http://docstore.mik.ua/orelly/webprog/DHTML_javascript/0596004672_jvdhtmlckbk-chp-10-sect-5.html
 
 
  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">
</FORM>

 
     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">
</FORM>

<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));

</SCRIPT>


More @ :
http://www.developer.com/lang/jscript/article.php/639611/Passing-Variables-Between-Pages-With-JavaScript.htm

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 @ :
http://pablotron.org/?cid=1557