11487 sujets

JavaScript, DOM et API Web HTML5

Bonjour à tous,

Je cherchais initialement un javascript en rapport avec un fichier xml mais comme je n'ai pas trouvé, je me suis rabattu sur le javascript ci-dessous qui intégre ma "BDD".

Il fonctionne parfaitement mais j'aimerais faire en sorte qu'il fasse la recherche sur plusieurs mots clé saisies dans le même champs.

Il doit y avoir une boucle et une extraction à faire sur la saisie mais je ne sais pas faire....
Quelqu'un pourrait il m'aider?


<SCRIPT language=javascript>
// Script développé par Olivier Hondermarck   
// D'autres scripts sur  http://www.toutjavascript.com
 
// Utilisation gratuite à condition de laisser ces commentaires

// Création de la base de données
var tjs_base=new TJS_InitBase("_self");
// TJS_InitBase(target) : target="_blank" pour ouvrir dans une nouvelle page

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


tjs_base.Add("URL","Ceci est un légume","carotte haricot salade ");
tjs_base.Add("URL","<B></B>","");
tjs_base.Add("URL","Ceci est un fruit","banane peche orange");










////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////







// COMPLETEZ ICI la base de données
// tjs_base.Add(page,titre,mots-clés)
String.prototype.sansAccent = function(){
    var accent = [
        /[\300-\306]/g, /[\340-\346]/g, // A, a
        /[\310-\313]/g, /[\350-\353]/g, // E, e
        /[\314-\317]/g, /[\354-\357]/g, // I, i
        /[\322-\330]/g, /[\362-\370]/g, // O, o
        /[\331-\334]/g, /[\371-\374]/g, // U, u
        /[\321]/g, /[\361]/g, // N, n
        /[\307]/g, /[\347]/g, // C, c
    ];
    var noaccent = ['A','a','E','e','I','i','O','o','U','u','N','n','C','c'];
     
    var str = this;
    for(var i = 0; i < accent.length; i++){
        str = str.replace(accent[i], noaccent[i]);
    }
     
    return str;

}

function TJS_InitBase(target) {
	this.nb_item=0;
	this.target=target;
	this.Add=AddItem;
	this.Search=SearchItem;
}
function AddItem(page,desc,cle) {
	var nb = this.nb_item;
	var item = new Object;
		item.page=page;
		item.desc=desc;
		item.cle=cle;
	this[nb]=item;
	this.nb_item++;
}


function SearchItem(txt) {
// txt contient le texte de la recherche
////////////////////////////////////////////////	
//	txtsaisie=txt;
//	alert( txtsaisie );
////////////////////////////////////////////////	
	txt = unescape(txt);

////////////////////////////////////////////////	
	txt=txt.sansAccent()
	//txt.match(/("[^"]*")|([^\s]+)/g);
	//alert( txt );
////////////////////////////////////////////////	

	txt = txt.replace(/\+/g," ");
	var n=tjs_base.nb_item;
	var indice=-1;
	if (txt=="") {alert("Entrez un mot pour la recherche"); }
	else{	
	    document.forms["tjs_search"].elements["mot"].value=txt; ///	    document.forms["tjs_search"].elements["mot"].value=txtsaisie;
		var Z=""; var nb=0;
		for (var i=0; i<n; i++) {
			if (tjs_base[i].cle.toUpperCase().indexOf(txt.toUpperCase(),0)!="-1") {
				Z+=tjs_base[i].desc + "<BR><A ' href='"+tjs_base[i].page+"'>"+tjs_base[i].page+"</A><BR><BR><table width=800 height=3><tr><td style=\'background-color : #B0E0E6\'></td></tr></table>"
				nb++;
			}
		}
		if (nb>0) {
			if (nb==1) {Z="Un résultat trouvé pour la recherche sur [<B>"+txt+"</B>] : <BR><BR><table width=800 height=3><tr><td style=\'background-color : #B0E0E6\'></td></tr></table>"+Z;}
			else {Z=nb+" résultats trouvés pour la recherche sur [<B>"+txt+"</B>] : <BR><BR><table width=800 height=3><tr><td style=\'background-color : #B0E0E6\'></td></tr></table>"+Z;}
		}else{Z="Aucun résultat pour la recherche sur [<B>"+txt+"</B>]<BR>Vérifiez l'orthographe ou essayez un autre mot clé !";}
		document.write(Z);
	}
}

function TJS_Search(f) {
	if (f.mot.value!=""){f.submit();}	
}

function TJS_PrintResult() {
	var url=document.location.href;
	if (url.indexOf("?mot=",0) > 0) {
		var mot=url.substring(url.indexOf("?mot=",0)+5);
		SearchItem(mot);
	}
}

</SCRIPT>


[/i][/i][/i][/i][/i][/i]
Modifié par benjea06 (18 Apr 2014 - 12:57)