Jump to content

User:JohnDR/jsexp

From Wikipedia, the free encyclopedia

<<<< Back to jsmain

HTML/DOM

[edit]

Button Animation

[edit]
<html>
<head>
<script type="text/javascript">
function mouseOver() {
  document.b1.src ="first.gif"
}
function mouseOut() {
  document.b1.src ="second.gif"
}
</script>
</head>

<body>
<a href="http://www.w3schools.com" target="_blank">
<img border="0" alt="Visit W3Schools!" src="b_pink.gif" name="b1"
     onmouseOver="mouseOver()" onmouseOut="mouseOut()" />
</a>
</body>
</html>

Form Validate

[edit]
<script type="text/javascript">
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);
    }
  }
}
</script>

<body>
<form action="submitpage.htm" 
      onsubmit="return validate_form(this);"
      method="post"> Email:
<input type="text" name="email" size="30">
<input type="submit" value="Submit">
</form>
</body>

javascript version html test

[edit]
  • ff2.0 is javascript1.7
  • ie6.0 is javascript1.3
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- This script is from http://javascript.internet.com/user-details/javascript-version.html -->

<!-- Begin
jsver = "1.0";
// End --></SCRIPT>
<SCRIPT LANGUAGE="JavaScript1.1"><!-- Begin
jsver = "1.1";
// End --></SCRIPT>
<SCRIPT Language="JavaScript1.2"><!-- Begin
jsver = "1.2";
// End --></SCRIPT>
<SCRIPT Language="JavaScript1.3"><!-- Begin
jsver = "1.3";
// End --></SCRIPT>
<SCRIPT Language="JavaScript1.4"><!-- Begin
jsver = "1.4";
// End --></SCRIPT>
<SCRIPT Language="JavaScript1.5"><!-- Begin
jsver = "1.5";
// End --></SCRIPT>
<SCRIPT Language="JavaScript1.6"><!-- Begin
jsver = "1.6";
// End --></SCRIPT>
<SCRIPT Language="JavaScript1.7"><!-- Begin
jsver = "1.7";
// End --></SCRIPT>
<SCRIPT Language="JavaScript1.8"><!-- Begin
jsver = "1.8";
// End --></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>

<!-- STEP TWO: Add the last coding to the BODY of your HTML document  -->
<BODY>
<SCRIPT LANGUAGE="JavaScript"><!-- Begin
document.write("<B>Your browser supports JavaScript version " + jsver + ".</B>")
// End --></SCRIPT>

Goodies

[edit]

Netflix online rent simulation

[edit]

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

[edit]

array being released in memory

[edit]
  • 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.
<html><head><script type="text/javascript">
var c=0, t;

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


</script></head><body>
<form>
<input type="button" value="Start count!" onClick="timedCount()">
<input type="text" id="txt">
<input type="button" value="Stop count!" onClick="stopCount()">
</form><p>
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.
</p></body></html>



Other Archives

[edit]

sortkeys / sorthashvalue

[edit]
  • sortkeys
// 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);

  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);
}
  • sorthashvalue
// 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);
  }

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

  return(nvh);
}