User:JohnDR/jsexp

<<<< Back to jsmain

= HTML/DOM =

Button Animation
 function mouseOver { document.b1.src ="first.gif" } function mouseOut { document.b1.src ="second.gif" }

  

Form Validate
 function validate_email(field, alert_txt) { with (field) { apos=value.indexOf("@") dotpos=value.lastIndexOf(".") if (apos<1||dotpos-apos<2) { alert(alert_txt); return false } else { return true; } } }

function validate_form(thisform) { with (thisform) { if (validate_email(email,"Not valid e-mail address!")==false) { email.focus; return(false); } } }

 Email:  

javascript version html test
 
 * ff2.0 is javascript1.7
 * ie6.0 is javascript1.3

   </SCRIPT> <SCRIPT Language="JavaScript1.4"></SCRIPT> <SCRIPT Language="JavaScript1.5"></SCRIPT> <SCRIPT Language="JavaScript1.6"></SCRIPT> <SCRIPT Language="JavaScript1.7"></SCRIPT> <SCRIPT Language="JavaScript1.8"></SCRIPT>

<NOSCRIPT> <B>Your browser does not currently support JavaScript.</B> <P><HR><P> With Internet Explorer 5: Go to Tools Menu|Internet Options, Security Tab  and click on the Custom Level. Then select disable active scripting under the scripting section. </NOSCRIPT>

<BODY> <SCRIPT LANGUAGE="JavaScript"></SCRIPT>

= Goodies =

Netflix online rent simulation
// definitions here var maxdisc=2;

var hmin=new Array;    // hmin is the min day of a state var hmax=new Array;    // hmax is the max dat of a state var hh=new Array;      // hh is the state of a particular disk var hhd=new Array;     // hhd contains the n days for a particular disk var hhr=new Array;     // contains the random number

// state 0 - in netflix // state 1 - in mail // state 2 - in house // state 3 - in mail hmin[0] = 1; hmax[0]=2; hmin[1] = 1; hmax[1]=2; hmin[2] = 2; hmax[2]=5; hmin[3] = 1; hmax[3]=2;

var i,dy=0; var tot=0;

for(i=0;i<maxdisc;i++) { // one disc at a time hh[i]=0; hhd[i]=0; hhr[i]=getrand(hh[i]); }

// process one day at a time for(dy=0;dy<100;dy++) { for(i=0;i<maxdisc;i++) { // one disc at a time

//  print("Day "+dy+": Disc "+i+" is in state "+hh[i]+", max="+hhr[i]);

// increment the day hhd[i]++; if(hhd[i]==hhr[i]) { hh[i]++;  // advance the state if(hh[i]==4) hh[i]=0; if(hh[i]==2) tot++; hhr[i]=getrand(hh[i]); hhd[i]=0; } }

print("Day "+dy+": Discs "+cntit+" Total Rented: "+tot); }

function getrand(state) { var j; if(state==2) { switch(cntit) { case 0: case 1: hmin[2]=2; hmax[2]=5; break; case 2: hmin[2]=3; hmax[2]=6; break; case 3: hmin[2]=4; hmax[2]=7; break; default: hmin[2]=5; hmax[2]=8; break; }   hmin[2]=1; hmax[2]=2; }

j=Math.floor(Math.random*1000) % (hmax[state]-hmin[state]+1); j=j+hmin[state]; return(j); }

function cntit { var cnt,i; cnt=0; for(i=0;i<maxdisc;i++) { if(hh[i]==2) cnt++; } return(cnt); }

= Benchmark Scripts =

array being released in memory
<script type="text/javascript"> var c=0, t;
 * Summary: YES! firefox/ie releases the memory allocated (local variable) inside the function.
 * refer to "var thh" below:
 * If the "glb=hh.keys;" is changed to "glb.push(hh.keys);", then memory will continuously go up!!! This means that variables are released smartly.

function myrand(limit) {    // limit is optional. Default is 1000000 // Need 30% more to cover all combinations. For example, if limit=100, you need 130 iterations to get all 100 combinations. var initialseed=7; var thelimit = limit ? limit : 1000000; var sum=initialseed; var cntr=0;

this.myrand = function { sum = sum*71 + 3;     // 49 is letter "a", 73 is 122-49. however, 71 has more unique combination sum = sum % thelimit;  // 250000 combinations if(sum==initialseed) cntr++; return((sum+cntr) % thelimit); } this.getcntr = function { return(cntr) }; }

function hash(ini) { var ownfunc=new Array; var ii;

this.keys = function { var thh=new Array; ccc=0; for(ii in this) { ccc++; if(ii in ownfunc) continue; thh[ii]=this[ii]; }    return(thh);   // same as this without the methods }  for(ii in this) ownfunc[ii]=1;    // save all known methods for(ii in ini) this[ii]=ini[ii];  // initial keys }

function timedCount { ccc=0; glb=hh.keys; document.getElementById('txt').value=c+" "+ccc; c=c+1 t=setTimeout("timedCount",100) }

function stopCount { clearTimeout(t); }

var hh=new hash; var rr=new myrand(10000000); // create hash of 900000 keys for(i=0;i<900000;i++) { hh['a'+rr.myrand]=1; }

<input type="button" value="Start count!" onClick="timedCount"> <input type="text" id="txt"> <input type="button" value="Stop count!" onClick="stopCount"> Click on the "Start count!" button above to start the timer. The input field will count forever, starting at 0. Click on the "Stop count!" button to stop the counting.

= Other Archives =

sortkeys / sorthashvalue
// returns the sorted keys given a hash Array // equivalent to "foreach $v (sort(keys(%src)))" in perl // Use this to sort a normal "for (i in array); ->  for (i in sortkeys(array));" // Rev2: improved version. Can assign "hh=sortkeys(hh);" function sortkeys(src, fun) {       // fun is the sort function. optional var nv = new Array; var nvh = new Array;       // works ok bec of "closure" var i; for (i in src) nv.push(i);
 * sortkeys

if(fun==undefined) { nv.sort; } else { nv.sort(fun); } for (i in nv) { nvh[nv[i]]=src[nv[i]];      // Now transfer it to the keys }

return(nvh); }

// returns the sorted values. // DOES NOT modify the src array // e.g. aa["z"]=9; aa["zz"]=8; aa["a"]=2;  for (i in sorthashvalue(aa)) { print(i); }    // RETURNS 2, 8, 9 // Rev1: initial rev function sorthashvalue(src, fun) {  // fun is the sort function. optional var nv = new Array; var nvh = new Array;       // works ok bec of "closure" var cnt=0; var i; for (i in src) { nv[cnt++]=src[i]; } if(fun==undefined) { nv.sort; } else { nv.sort(fun); }
 * sorthashvalue

for (i in nv) { nvh[nv[i]]=1;      // Now transfer it to the keys }

return(nvh); }