chaine =chaine.replace(/[ÈÉÊËèéêë]/g,"e");
chaine=chaine.toLowerCase();
chaine=chaine.replace(/é/g, "e");
/* ULTRA : Convertir une variable en method ou fonction */
Problème : Je veux obtenir :
json.myMethod1
Contrainte : J'ai que :
maChaine = "myMethod1";
Solution : eval
monObj = eval("var json." + maChaine);
=> monObj équivaut à json.myMethod1
distance = Math.ceil(distance);
=> Ex: 12.568 devient 13
var nombres = new Array(1, 2, 5, 8, 9, 12, 16);
nombres.sort();
alert(nombres);
/*------ input -----*/
-> Sélectionner le contenu d'un champ :
/*------ Résolution d'écran ------*/
/*------ Redirection ------*/
window.history.go(-2)
/*-----
Bouton d'envoi normal :
Bouton sans envoi :
/*-- array() --*/
Boucler sur un tableau
var items = {
"foo" : 123456,
"bar" : 789012,
"baz" : 345678,
"bat" : 901234
};
for(var index in items) {
document.write( index + " : " + items[index] + " ");
}
Vérifier si c'est un tableau [typeof]
var fruits = ["Banana", "Orange", "Apple", "Mango"];
typeof fruits;
équivaut à :
function isArray(myArray) {
return myArray.constructor.toString().indexOf("Array") > -1;
}
/************************************************/
/* General */
/************************************************/
javascript:window.opener.location.reload()
/** Attendre **/
setTimeout("alert ('test');",2000); // 2 secondes
document.getElementById("patience").style.visibility="visible";
document.getElementById("patience").style.visibility="hidden";
/** GetEtlementByTagName **/
var x = document.getElementsByTagName("img").item(0).getAttribute("src");
-> Remplace une image par une autre
document.getElementsByTagName("img").item(2).setAttribute("src", v);
/** onClick **/
onblur="javascript:alert('blur')" // on tape du text et on clique autre part
/** Changer une image **/
function changeImage() {
//alert("test");
var x = document.getElementsByTagName("img").item(6).getAttribute("src");
url_image = "images/logo_ac.jpg";
//document.getElementsByTagName("img").item(2).setAttribute("src", url_image); // ok : on remplace la source de l'image
document.getElementById('blocLogo').innerHTML=''; // ok : on remplace le contenu de la balise div
document.getElementById('blocChaussette').innerHTML='';
alert(x);
}
/** Hoster **/
var host = location.host; // Le host (http://www.lexmark.com)
/** Redirect **/
/** Redirect **/
/** Prompt **/
function confirmdelete(){
if (confirm("Do you really want to delete this article?")){
document.forms['form2'].submit();
}
}
/** Referrer **/
document.referrer
/** Get the file name from the URL **/
url="http://www.lexmark.fr/uncomplicate/channel/home/0,7057,256846_2721_0_fr,00.html";
var urlVars=url.split("/");
alert(urlVars[urlVars.length-1]);
/** Open window **/
onclick="window.open('http://test.com/page.html','WindName','height=400,width=400,resizable=no,status=no,toolbar=no,menubar=no,location=no,left=100,top=10')"
Info
/** Lire URL **/
http://www.bebc.fr/lectureurl.html?nom=donny
- location.href
=> http://www.bebc.fr/lectureurl.html?nom=donny
- location.host
=> donnera web.covertprestige.info
- location.hostname
=> web.covertprestige.info
- location.pathname
=> /divers/test.html
- location.search
=> ?machin=test
- chaine = document.location.hash.substring(1,location.hash.length);
=> Affiche nom=donny
- Afficher les paramètres après l'URL (après le ?) :
var parameters = location.href.split("?");
alert(parameters[1]);
=> Affiche nom=donny
/************************************************/
/* Script de décompte time */
/************************************************/
/************************************************/
/* URL et Chaine */
/************************************************/
/************************************************************************/
/* Chaine */
/* http://www.w3schools.com/jsref/jsref_obj_string.asp */
/************************************************************************/
/* en minuscule */
mavariable = mavariable.toLowerCase()
/** Retrouve une chaine dans une autre (test si chaine contient une autre chaine)
mail="nguyen@lexmark.com";
mail.indexOf("@")>=0
=>
/** Incrementation **/
cool="test";
cool+="46546";
/** length **/
_mystring="Bonjour";
_result = _mystring.length;
=> _result contient 7
/** search a string **/
_mystring="Salut mec";
alert(_mystring.indexOf('mec')); // return 6
alert(_mystring.indexOf('Salut')); // return 0
alert(_mystring.indexOf('Bonjour')); // return -1
alert(_mystring.search(/Salut/)); // return 0
/** replace **/
if(host.indexOf('www.')>=0) host=host.replace(/www./, "");
/*++ Les premiers caractères ++*/
var date = "15/08/2000";
var jour = date.substring(0,2);
var mois = date.substring(3,5);
var annee = date.substring(6,10);
var test = "donny nguyen";
alert( test.substring(5) );
=> retourne "nguyen"
/* remplacer un espace par autre chose */
inCity = inCity.replace(/ /g, '-');
/************************************************/
/* Tableaux simple */
/************************************************/
/* Déclarer un tableau */
var _mytable = [];
var MonTableau = ["donnée 1", "donnée 2", "donnée 3", "donnée 4"];
var MonTableau = new Array("donnée 1", "donnée 2", "donnée 3", "donnée 4");
var MonTableau = ["Teebo", "Eaulive", "Asevere", "Kalamit", "Serge", "Chat_Teigne", "BmV"];
document.write("Le 4ième élément du tableau est "+MonTableau[3]); // Affichera "Le 4ième élément du tableau est Kalamit"
MonTableau[0] = "Bonjour";
MonTableau["Pierre"] = 12;
MonTableau["Jean-François"] = 25;
/* Taille d'un tableau */
MonTableau.length
/************************************************/
/* Tableaux avec clefs */
/************************************************/
/* Declare */
_table = {"a" : "Athens", "b" : "Belgrade", "c" : "Cairo" };
for (key in _table)
document.write("For the key " + key
+ " element value is " + _table[key]);
/* search a key */
var _table = [];
_table["lexmark.fr"] = "France";
if("lexmark.de" in _table) alert("good");
else alert("bad");
/************************************************/
/* Control de formulaire simple */
/************************************************/
/************************************************/
/* Control de formulaire */
/************************************************/
/** Formulaire **/
doc=document.forms[0];
alert(doc.elements['firstname'].value);
/** Checkbox et radio control **/
myform.elements.length
=> Nombre d'élément du formulaire
/** Checkbox et radio control **/
myform : nom du formulaire
place : non du champs radio
nb_click=0;
for(i=0; i
/************************************************/
/* Operateurs */
/************************************************/
Mon div :
JS : var obj = document.getElementById("donny");
alert(obj.style.visibility);
=> Affiche "hidden"
/************************************************/
/* Operateurs */
/************************************************/
switch (var)
{
case '1':
case '2':
case '3': alert ('1, 2 ou 3');
case '4':
case '5':
case '6': alert ('4, 5 ou 6');
default: alert('autre');
}
/************************************************/
/* select jump */
/************************************************/
/************************************************/
/* timer / temps */
/************************************************/
Fonction : setInterval()
setInterval(function(){alert("Hello")},3000);
=> Affiche Hello toutes les 3 secondes
Fonction : setTimeout()
function fctGo() {
alert('hello');
}
setTimeout("fctGo()", 5000);
=> Lance la fonction au bout de 5 secondes
/************************************************/
/* Cacher une balise DIV */
/************************************************/
document.getElementById('nomBalise').style.visibility = 'hidden';
document.getElementById('resultat').style.visibility = 'visible';
/*++++++++++ Supprimer les espaces d'une chaine ++++++++++*/
var chaine='suppression des espaces dans ce texte';
chaine = chaine.replace(/ /g,"");
alert (chaine);
/*++++++++++ Vérifier que la chaine ne comporte que des chiffres ++++++++++*/
function checkPostcode(monTexte) {
reg = new RegExp('[^0-9]+', 'g');
if(reg.test(valeur)) {
monTexte=monTexte.replace(/[^0-9]+/, '');
} else {
return true;
}
}
/*++++++++++ Compte le nombre de input text ++++++++++*/
function fctNbInputText() {
myform=document.score_form;
nbInput=myform.elements.length;
nbInputText=0;
for(j=0; j=0)) {
nbInputText++;
}
}
alert(nbInputText);
}
/*------ Cookies simples ------*/