JAVASCRIPT

  http://developer.netscape.com/docs/manuals/javascript.html

  ECMA: le standard définissant Javascript
Il y a un débuggueur javascript dans Netscape (enfin, dison plus précisément qu'il peut afficher des messages d'erreur) : il suffit de lui demander d'aller voir l'URL
  <javascript:>

JAVASCRIPT ET HTML
Javascript dans une page HTML
  <SCRIPT LANGUAGE="Javascript1.2">
    ...
  </SCRIPT>
Attributs Javascript
  <HR WIDTH="&{barWidth};%">
Quelques évènements:
  onBlur onFocus
  onChange
  onClick
  onLoad onUnload
  onMouseOver
  onReset onSubmit
  onResize

  <INPUT TYPE="button" NAME="Button1" VALUE="Open Sesame!"
         onClick="window.open('1.html', 'newWin')>

  <FORM>
  <INPUT TYPE="text" NAME="phone" onChange="textInfo(this)">
  </FORM>
On peut changer ce qu'il faut faire lors d'un évènement
  function fun2 () {...}
  document.form1.button1.onclick = fun2;

OBJETS DEFINIS PAR LE NAVIGATEUR
  window
  frame
  document
  location
  history
  navigator
EXEMPLES
  document.myform.text.value
  document.aircraft.src = '1.gif';
  document.title
  document.bgColoe
  document.fgColor
  document.linkColor
  document.alinkColor
  document.vlinkColor
  document.referrer
  document.cookie

  location.href
  location.reload
  location.replace

  history.length
  history.go(-2)

  window.open
  window.close
  window.blur
  window.focus
  window.setInterval
  window.setTimeout

  document.anchors[]
  document.applets[]
  document.embeds[]
  document.forms[]
  document.forms[0].elements[]
  document.images[]
  document.layers[]
  document.links[]

  select.options[]

HIERARCHIE GENERALE
    <script  language="Javascript1.2">
      function parcourrir(n, base, object) {
        if( n>4 ){ document.write( "TOO DEEP<BR>\n" ); } else {
          for (var i in object) {
            document.write(base +"."+ i +"("+typeof(object[i])+")<BR>\n");
            if( typeof( object[i] ) == "object" ){
              parcourrir(n+1, base+"."+i, object[i]);
            } else {
              document.write("-->"+ object[i] +"<BR>\n");
            }
          }
        }
      }
      parcourrir(1, "document", document);
      document.write("<P><HR><HR><HR><P>");
      parcourrir(1, "history", history);
      document.write("<P><HR><HR><HR><P>");
      parcourrir(1, "navigator", navigator);
      document.write("<P><HR><HR><HR><P>");
      parcourrir(1, "window", window); // Récursivité ???
      document.write("<P><HR><HR><HR><P>");
      parcourrir(1, "frames", frames);
    </script>
Voici le résultat
  <javascript.hierarchie.html>

VARIABLES
La déclaration des variables est facultative.
  var toto;
  var toto = "abc";
  for(var i; i<10; i++)
Quelques constantes
  true
  false
  null
  undefined
Chaines de caractères
  s = "abc" + 'cde';
  s = "\251 \xA9 \u0049"; // latin1 octal, latin1 hex, unicode
  s = escape("\e\t abc");
  ss = unsecape(s);
Tables de hachage
  car = { a: 1, b: 2, c: "abc" };
  document.write(car.c);
  document.write(car["c"]);
  delete car.a;
Tableaux
  l = [0,1,2,3];
  l = [0,1,2,3,]; // idem
  l = [0, , ,3]; // l[1] et l[2] sont vides
  delete l[3];
Expressions régulières
  re = /a+bc/;
  re = new RegExp("a+bc");

  re = /(\w+)\s(\w+)/;
  str = "John Smith";
  newstr = str.replace(re, "$2, $1");

  myRe = /d(b+)d/g;
  myArray = myRe.exec("cdbbdbsbsd"); // [matched substr, remembered substr]
  myOtherArray = myRe.match("qskjdbbbdqdbdqkjs");
this: c'est l'objet courrant
typeof
void
  <A HREF="javascript:void(document.form.submit())">Submit</A>
Opérateurs
  + - * / % 
  += etc.
  && || !
  == != < > <= >= 
  === (égal et de même type)
  + (concaténation)
  & | ^ ~ (bitwise)

PROGRAMMATION
  if (...) {...}
  if (...) {...} else {...}
  for(var i=0; i<N; i++) {...}
  do {...} while (...)
  while (...) {...}
  with (obj) {...}
  for var i in obj {...}

  switch (...) { case...: ... break; case...: ... break; default: ... }

  function square(number) {return number*number;}
  square(5);

  // Fonction avec un nombre variable d'arguments
  function myconcat (separator) {
    my result = "";
    for( var i=1; i<arguments.length; i++ ){
      result += argument[i] + separator;
    }
    return resultat;
  }
  concat(",", "aaa", "bbb", "ccc");

OBJETS (tables de hachage)
  myObj.abc
  myObj[abc]

  for( var i in obj ) {...}

  myObj = {a: 123, b:"ABC", c:456};

  function f(x) {return x*this.a;}
  function Car(a,b,c){
    this.a = a;
    this.b = b;
    this.c = c;
    this.f = f;
  }
  var myCar = new Car(1,2,"abc");
  myCar.f(1);

  Car.prototype.color = null;
Terminologie :
  prototype (js) = classe (C++)
  objet     (js) = instance (C++)
  propriétés (js) = méthodes, membres (C++)
Le prototype définit les proproétés initiales d'un objet, mais on peut les changer par la suite.
On peut rajouter des propriétés à un protopype:
  Employee.prototype.foobar = null;
Héritage (uniquement simple):
  function Employee (...) {...}
  function Manager (...) {...}
  Manager.prototype = new Employee;
On peut récupérer le protopype d'un objet :
  obj.__proto__
ainsi que celui de ses ancètres :
  obj.__proto__.__proto__
  obj.__proto__.__proto__.__proto__
  etc.

OBJETS PREDEFINIS
  a = new Array(5); // tableau vide de longueur 5
  a = new Array(0,1,2,3,4);
  a = [0,1,2,3,4];
Méthodes de la classe Array :
  concat
  join
  pop
  push
  reverse
  shift
  unshift
  slice
  splice
  sort
Il y a un objet Date.
Les fonctions peuvent être vues comme des objets.
Méthodes de la classe String:
  anchor blink big bold fixed italics small strike sub sup link (formatage HTML)
  charAt
  charCodeAt
  indexOf
  lastIndexOf
  concat
  fromCharCode
  slice
  substring
  match search split replace (avec des expressions régulières)
Objet Math:
  PI
  abs sin cos tan asin acos atan exp log sqrt pow
  ceil floor round
  min max

ANIMATION
Appeler une fonction à intervalles réguliers
  setInterval
Appeler une fonction au bout d'un certain temps
  setTimeout

DIVERS
  homeWindow = window.open("1.html", "home", "toolbar=no,scroolbars=yes");
  window.close();
  self.close();
  close();
  homeWindow.close();

  onClick="top.frames[0].location = '1.html'"

  <A HREF="1.html" TARGET="contentFrame">...</A>

URLs JAVASCRIPT
  <A HREF="javascript:history.go(0)">Reload</A>

CLIENT-SIDE IMAGE MAPS
  <MAP NAME="buttonbar">
    <AREA SHAPE="RECT" COORDS="0,0,16,14"
          HREF="javascript:top.close(); window.location='new.html'">
    <AREA SHAPE="RECT" COORDS="0,0,16,14"
          HREF="contents.html"
          TARGET="javascript:alert('loading contents'); window.location='contents.html'">
  </MAP>

STATUS BAR
  defaultStatus = "...";

  <A HREF="..." onMouseOver="window.status='...'; return true;">...</A>

COOKIES
(Pas lu)

SECURITE
(Pas lu)

LIVECONNECT
On peut mélanger Java et JavaScript

AUDIO
  <EMBED  WIDTH=320 HEIGHT=256 SRC="1.wav"  HIDDEN=TRUE>
  <A HREF="javascript:document.embeds[0].play(false);">Play</A>

  <EMBED  WIDTH=320 HEIGHT=256 SRC="1.wav"  HIDDEN=TRUE NAME="firstsound" MASTERSOUND>
  <A HREF="javascript:document.firstsound.play(false);">Play</A>

  <EMBED  WIDTH=320 HEIGHT=256 SRC="1.wav"  HIDDEN=TRUE NAME="firstsound" MASTERSOUND>
  <A HREF="javascript:document.firstsound.play(false,'2.wav');">Play</A>

Vincent Zoonekynd
<zoonek@math.jussieu.fr>
latest modification on mar jun 26 14:45:58 CEST 2001