| Retour au sommaire |
JavaScript - Index et astuces


[Commandes utiles] [Liens utiles]


Commandes ou fonctions utiles :

  • Objet Location (IE et Netscape)
    protocol//host:port/pathname#hash?search
    
  • Récupération d'une valeur de la QUERY_STRING (attention pour IE, il faut un serveur Web local)
    function getValueInString(Name)
    {
            // Generic function to search for a field in a string containing several
            //  e.g. f1=val1&f2=val2 separated by delimiter "&" or whatever
            var obj = location.search; // ou document.URL // raw source field 
            var search = Name + "="; // search string
            var delimiter = "&"; // delimiter between fields or end of field
            if (obj.length > 0)
            {
                    offset = obj.indexOf(search);
                    if (offset != -1)
                    {
                            // if search exists
                            offset += search.length;
                            // set index of beginning of value
                            end = obj.indexOf(delimiter, offset);
                            // set index of end of value
                            if (end == -1)
                                    end = obj.length;
                            return unescape(obj.substring(offset, end));
                    }
            }
            return "";
    }
    
  • Manipuler des dates
      myDate = new Date();
      tomorrowDate = myDate.getDate() + 1;
      myDate.setDate(tomorrowDate);
  • Positionner des cookies
      // Sets cookie values. Expiration date and path are optional//
      function setCookie(name, value, expire, path)
      {
            // Bien qu'ici on ait un '=' et pas un '+=', positionner plusieurs cookies fonctionne
            document.cookie = name + "=" + escape(value)
                    + ( (expire == null) ? "" : ("; expires=" + expire.toGMTString()) ) 
                    + ( (path == null) ?  "" : ("; path=" + path) );
      }
    
      // Notice the use of escape to encode special characters (semicolons, commas, spaces) in the value string.
      //  This function assumes that cookie names do not have any special characters. The following function returns
      //   a cookie value, given the name of the cookie:
      function getCookie(Name)
      {
             var search = Name + "=";
             if (document.cookie.length > 0)
             {
                  // if there are any cookies
                  offset = document.cookie.indexOf(search);
                  if (offset != -1) { // if cookie exists
                       offset += search.length // set index of beginning of value
                       end = document.cookie.indexOf(";", offset); // set index of end of cookie value
                       if (end == -1)
                           end = document.cookie.length;
                       return unescape(document.cookie.substring(offset, end))
                  }
              }
       }
  • liste des propriétés d'un objet (ex. objet location pour l'URL)
       function dumpURL(type)
       {
            /* Affiche les valeurs des variables
             *  dans une fenetre alert si type = "ALERT" et
             * a l'endroit dans la page sinon
             */
            var msg = "";
            var sepNewLine = "--CRLF--"; // Si modif, changer aussi l'expression reg
            var myRegExp = /--CRLF--/g;
    
            msg = showProps(location, "location", sepNewLine);
    
            if (type == "ALERT")
                    alert(msg.replace(myRegExp , "\n"));
            else
                    document.write(msg.replace(myRegExp, "<br>"));
       }
    
       function showProps(obj, objName, sepNewLine)
       {
            var result = "";
            for (var i in obj)
                    result += objName + "." + i + " = " + obj[i] + sepNewLine;
            return result;
       }

    Liens utiles (voir ma page de liens) :


    [Commandes utiles] [Liens utiles]

    Date de modification : 1er juillet 2000