Transformer 1 liste déroulante en 1 javascrip

Fermé
webmaster73 - 20 sept. 2002 à 15:12
Bobinours Messages postés 2898 Date d'inscription jeudi 26 avril 2001 Statut Membre Dernière intervention 21 mars 2013 - 24 sept. 2002 à 01:53
Bonjours, je voudrais transformer cette liste déroulante dont le code source est (sans les tags xmp bien sure) :

<xmp>

<FORM name=menu1><SELECT class=sel name=service
onchange="if (this.options[this.selectedIndex].value.length!=0) window.open(this.options[this.selectedIndex].value,'centre')">
<OPTION selected value="">quelques sites</OPTION>
<OPTION value="">------------------</OPTION>
<OPTION value= http://www.vega.fr.st>art</OPTION>
<OPTION value= http://www.augalop.fr.st>cheval</OPTION>
<OPTION value="">-----------------</OPTION></SELECT>
<SCRIPT
language=Javascript><!-- document.menu1.service.selectedIndex=0;//--></SCRIPT>
</FORM>

</xmp>

donc ce code source en 1 javascript externe qui sera indépendant de ma page web mais que je pourrais faire apparaitre comme une liste normal. J'espère avoir réussi à exposer ce que j'ai besoin merci d'avance...

3 réponses

Bobinours Messages postés 2898 Date d'inscription jeudi 26 avril 2001 Statut Membre Dernière intervention 21 mars 2013 504
21 sept. 2002 à 13:06
Salut,
Fais comme ceci :

# FICHIER : externe.js
########################

document.write('<FORM name="menu1">');
document.write('<SELECT class="sel" name="service" ');
document.write('onchange="if (this.options[this.selectedIndex].value.length!=0) window.open(this.options[this.selectedIndex].value,\'centre\')">');
document.write('<OPTION selected value="">quelques sites</OPTION>');
document.write('<OPTION value="">------------------</OPTION>');
document.write('<OPTION value=" http://www.vega.fr.st">art</OPTION>');
document.write('<OPTION value=" http://www.augalop.fr.st">cheval</OPTION>');
document.write('<OPTION value="">-----------------</OPTION>');
document.write('</SELECT>');
document.write('<SCRIPT language="Javascript" type="text/javascript"><!-- document.menu1.service.selectedIndex=0;//--></SCRIPT>');
document.write('</FORM>');


# FICHIER : page.htm
########################
<SCRIPT language="Javascript" type="text/javascript" src="externe.js"></SCRIPT>


-= Bobinours =-
La Belette Contrée : http://bobin.underlands.org/cgi-bin/bel
0
Merci, pour l'info ;-)
0
Bobinours Messages postés 2898 Date d'inscription jeudi 26 avril 2001 Statut Membre Dernière intervention 21 mars 2013 504
24 sept. 2002 à 01:53
J'ai négligé un point. Il faut préférer un seul document.write() à plusieurs (économie de fonction avec Entrée/Sortie vers l'écran.

Pour cela, deux astuces :

1) Affecter une variable, puis l'afficher :

var texte = '<FORM name="menu1">';
texte += '<SELECT class="sel" name="service" ';
texte += 'onchange="if (this.options[this.selectedIndex].value.length!=0) window.open(this.options[this.selectedIndex].value,\'centre\')">';
...


2) Afficher avec des concaténations :

document.write(''
+ '<FORM name="menu1">'
+ '<SELECT class="sel" name="service" '
+ 'onchange="if (this.options[this.selectedIndex].value.length!=0) window.open(this.options[this.selectedIndex].value,\'centre\')">'
+'');
...


Enfin, tu peux aussi construire la liste de toute pièce en javaScript, dans l'esprit Programmation Orientée Objet, mais je n'ai pas d'exemple.

-= Bobinours =-
Une Contrée? http://bobin.underlands.org/cgi-bin/belette.pl
0