Javascript tips

  • IE can give "object expected" error when you are calling Javascrupt function at load time -
    and that function itself is located lower on the page. Netscape doesn't have this problem.

  • "Error in line 1" usually means that file you are using in script src is missing.

  • Javascript may bomb if same field name is used several times in a form.

  • You can declare array withotu spercifying the size:
    var x=new Array(); x[1]=1; x[2]=2; The x.length will be set to 2 - index of last assigned element.

  • When you have number in a string variable, Javascript may mistake '+' operator
    for concatenation. Workaround: x = x - (-1) :)

  • You can read the caption of OPTION element using the .text property.

  • Calculating page load time (from DevEdge):
    <HEAD> <SCRIPT> Start = new Date()
    function NoteTime()
    {
    Stop = new Date()
    Diff = Stop.getTime() - Start.getTime(); alert(Diff); }
    </SCRIPT> </HEAD> <BODY onLoad='NoteTime()'> .....................
    </BODY>
  • IE can get confused and show strange messages when you
    have some plain HTML inside <SCRIPT> tag.

  • Code to close the window without confirmation:
    parent.window.close()

  • Checking whether form has a field:
    if (document.f.field1!=null)

  • Useful string functions:
    • s='a,bb,c'; r=s.split(','); This creates array r[0], r[1]... - works like PARSE VAR in REXX
    • charAt - like sunstring, except returns single cher
    • replace - useful func, but the syntax is somewhat cryptic.
      s="a b c bbBB"
      x=s.replace(/b/, "*"); - replace first occurence of b with *
      x=s.replace(/b/g, "*"); - replace all occurences except uppercase ones
      x=s.replace(/b/gi, "*"); - ignore case, replaces all 'b's and 'B's
      Nore = if you enclose first /b/ in quotes, it will not work!!
      Also, unless I am mistaken, this limits the usefulness of the function -
      because there doesn't seem a way to build the first parm via variable.
      Sure, you can say x=/b/, but that's not very useful. Try this:
      what='b'; x=/ + what + /; x=s.replace(/b/, "*")
      ... it doesn't work.

    • substring - 2nd parameter is indexr, while in subst, second parm
      is length
    • toLowerCase, toUpperCase - self-explanatory
    • search, match - funcs for locating regex within a string.


  • Everybody knows that you can break out of frame in HTML using "target=_top" in <a href>.
    You can do the same trick in JavaScript - to do this, instead of regular
    "location=" you will use "parent.location.href=".

  • Here's how to do escaping in javascript:
    document.write('a\' a');
  • Use escape() function to convert spaces into %20 in a string.
    IE and Opera wouldn't care either way, but Netscape may misbehave
    when JavaScript sets location with a space in it.

  • Javascript/forms - the language is case-sensitive (grrr), so
    the major cause of 'has no properties' error is specifying
    something like FORM NAME=XX instead of FORM NAME="xx".