IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

Appel d'un script js à partir d'une page html


Sujet :

JavaScript

  1. #1
    Membre éclairé
    Inscrit en
    Avril 2006
    Messages
    254
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 254
    Par défaut Appel d'un script js à partir d'une page html
    Bonjour,

    J'ai créé mon script: Hashtable.js et je voulais l'appelé en insérant cette ligne dans ma page html :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <script language='JavaScript' src='Hashtable.js' ></script>
    Le code de ma page html est le suivant:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <html>
     
     <title>Essaye d'appel des fichiers js</title>
     
      <script language='JavaScript' src='./js/Hashtable.js' ></script>
    <body>
     <script>
    alert("tt");
        var HT= new Hashtable();
        HT.put("lm","mm");
        HT.put("6",6);
        HT.put("7",7);
        var h=HT.get("lm");
      alert(h);
     </script>
     
     </body>
    </html
    Sachant que lorsque j'insére le code du script: Hachtabel.js dans ma page HTML, ça marche.

    voiçi ce que je tape ds mon browser:
    http://localhost:8080/examples/js3.html
    Les chemins de mes fichiers:
    C:\Tomcat 4.1\webapps\examples\js3.html
    C:\Tomcat 4.1\webapps\examples\js\Hashtable.js

    Et si j'essaye de placer "js3.html" sous "C:\Tomcat 4.1\webapps\examples\js\" , aussi ça ne marche pas, je crois que le problème vient d'ici"src='./js/Hashtable.js' >", le chemin peut être ou les slach. Si vous pouvez me résoudre ce prob ça sera gentil de votre part.

    Merci infiniment.

  2. #2
    Expert éminent

    Homme Profil pro
    Inscrit en
    Janvier 2007
    Messages
    13 474
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Secteur : Finance

    Informations forums :
    Inscription : Janvier 2007
    Messages : 13 474
    Par défaut
    Bonjour,
    tes balises <title> et <script> doivent être placées entre <head> et </head> (cf. cours HTML)

    A+

  3. #3
    Membre Expert Avatar de DoubleU
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    1 106
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 1 106
    Par défaut
    Enleve le ./ devant ./js/HashTable.js

    Sinon, si tu utilises l'extension pour FF "Web Développer", tu vas dans "Information", "Voir le javascript". Ca te donne tous les liens de fichiers javascript que tu tentes de charger. Si tu as une erreur dans tes paths, tu verras le chemin qu'il a essayé de charger et tu auras en dessous une erreur 404.

  4. #4
    Membre éclairé
    Inscrit en
    Avril 2006
    Messages
    254
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 254
    Par défaut
    J'ai mis les balises title et script entre les balise<head> et </head> mais ça ne marche tj pas.

  5. #5
    Expert éminent

    Homme Profil pro
    Inscrit en
    Janvier 2007
    Messages
    13 474
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Secteur : Finance

    Informations forums :
    Inscription : Janvier 2007
    Messages : 13 474
    Par défaut
    Citation Envoyé par developppez Voir le message
    J'ai mis les balises title et script entre les balise<head> et </head> mais ça ne marche tj pas.
    C'est à dire : quelles sont les erreurs, maintenant ?

    EDIT : et tu as modifié le path comme indiqué ?

    A+

  6. #6
    Membre éclairé
    Inscrit en
    Avril 2006
    Messages
    254
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 254
    Par défaut
    j'ai enlevé le ./, mais ça marche tj pas, il n'ya pas d'erreur, rien n'est affiché à part l'alert (tt)

  7. #7
    Membre éclairé
    Inscrit en
    Avril 2006
    Messages
    254
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 254
    Par défaut
    je ne comprend pas "FF", expliquez moi un peut svp:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Sinon, si tu utilises l'extension pour FF "Web Développer", tu vas dans "Information", "Voir le javascript". Ca te donne tous les liens de fichiers javascript que tu tentes de charger. Si tu as une erreur dans tes paths, tu verras le chemin qu'il a essayé de charger et tu auras en dessous une erreur 404.

  8. #8
    Membre Expert Avatar de DoubleU
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    1 106
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 1 106
    Par défaut
    FF c'est FireFox.

    L'extension dont je te parlais est ici

  9. #9
    Membre éclairé
    Inscrit en
    Avril 2006
    Messages
    254
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 254
    Par défaut
    Merci bcp doublu pour le lien, les erreurs sont les suivant:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    _uncaught exception: Permission refusée d'appeler la méthode Location.toString
    _Hashtable is not defined

  10. #10
    Expert éminent

    Homme Profil pro
    Inscrit en
    Janvier 2007
    Messages
    13 474
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Secteur : Finance

    Informations forums :
    Inscription : Janvier 2007
    Messages : 13 474
    Par défaut
    Reste donc plus qu'à voir le contenu de Hashtable.js, puisque c'est là que tout se passe ...

    A+

  11. #11
    Membre éclairé
    Inscrit en
    Avril 2006
    Messages
    254
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 254
    Par défaut
    C'est un peut long mais puisque vous voulez voir le code le voiçi:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    <script  language="JavaScript" >
    
    alert("jjjj");
    
    
    Hashtable.prototype.hash	 	= null;
    Hashtable.prototype.keys		= null;
    Hashtable.prototype.location	= null;
    
    /**
     * Hashtable - Constructor
     * Create a new Hashtable object.
     */
    function Hashtable(){
    	this.hash = new Array();
    	this.keys = new Array();
    
    	this.location = 0;
    }
    
    /**
     * put
     * Add new key
     * param: key - String, key name
     * param: value - Object, the object to insert
     */
    Hashtable.prototype.put = function (key, value){
    	if (value == null)
    		return;
    
    	if (this.hash[key] == null)
    		this.keys[this.keys.length] = key;
    
    	this.hash[key] = value;
    }
    
    /**
     * get
     * Return an element
     * param: key - String, key name
     * Return: object - The requested object
     */
    Hashtable.prototype.get = function (key){
    		return this.hash[key];
    }
    
    /**
     * remove
     * Remove an element
     * param: key - String, key name
     */
    Hashtable.prototype.remove = function (key){
    	for (var i = 0; i < this.keys.length; i++){
    		//did we found our key?
    		if (key == this.keys[i]){
    			//remove it from the hash
    			this.hash[this.keys[i]] = null;
    			//and throw away the key...
    			this.keys.splice(i ,1);
    			return;
    		}
    	}
    }
    
    /**
     * size
     * Return: Number of elements in the hashtable
     */
    Hashtable.prototype.size = function (){
        return this.keys.length;
    }
    
    /**
     * populateItems
     * Deprecated
     */
    Hashtable.prototype.populateItems = function (){}
    
    /**
     * next
     * Return: true if theres more items
     */
    Hashtable.prototype.next = function (){
    	if (++this.location < this.keys.length)
    		return true;
    	else
    		return false;
    }
    
    /**
     * moveFirst
     * Move to the first item.
     */
    Hashtable.prototype.moveFirst = function (){
    	try {
    		this.location = -1;
    	} catch(e) {/*//do nothing here :-)*/}
    }
    
    /**
     * moveLast
     * Move to the last item.
     */
    Hashtable.prototype.moveLast = function (){
    	try {
    		this.location = this.keys.length - 1;
    	} catch(e) {/*//do nothing here :-)*/}
    }
    
    /**
     * getKey
     * Return: The value of item in the hash
     */
    Hashtable.prototype.getKey = function (){
    	try {
    		return this.keys[this.location];
    	} catch(e) {
    		return null;
    	}
    }
    
    /**
     * getValue
     * Return: The value of item in the hash
     */
    Hashtable.prototype.getValue = function (){
    	try {
    		return this.hash[this.keys[this.location]];
    	} catch(e) {
    		return null;
    	}
    }
    
    /**
     * getKey
     * Return: The first key contains the given value, or null if not found
     */
    Hashtable.prototype.getKeyOfValue = function (value){
    	for (var i = 0; i < this.keys.length; i++)
    		if (this.hash[this.keys[i]] == value)
    			return this.keys[i]
    	return null;
    }
    
    
    /**
     * toString
     * Returns a string representation of this Hashtable object in the form of a set of entries,
     * enclosed in braces and separated by the ASCII characters ", " (comma and space).
     * Each entry is rendered as the key, an equals sign =, and the associated element,
     * where the toString method is used to convert the key and element to strings.
     * Return: a string representation of this hashtable.
     */
    Hashtable.prototype.toString = function (){
    
    	try {
    		var s = new Array(this.keys.length);
    		s[s.length] = "{";
    
    		for (var i = 0; i < this.keys.length; i++){
    			s[s.length] = this.keys[i];
    			s[s.length] = "=";
    			var v = this.hash[this.keys[i]];
    			if (v)
    				s[s.length] = v.toString();
    			else
    				s[s.length] = "null";
    
    			if (i != this.keys.length-1)
    				s[s.length] = ", ";
    		}
    	} catch(e) {
    		//do nothing here :-)
    	}finally{
    		s[s.length] = "}";
    	}
    
    	return s.join("");
    }
    
    /**
     * add
     * Concatanates hashtable to another hashtable.
     */
    Hashtable.prototype.add = function(ht){
    	try {
    		ht.moveFirst();
    		while(ht.next()){
    			var key = ht.getKey();
    			//put the new value in both cases (exists or not).
    			this.hash[key] = ht.getValue();
    			//but if it is a new key also increase the key set
    			if (this.get(key) != null){
    				this.keys[this.keys.length] = key;
    			}
    		}
    	} catch(e) {
    		//do nothing here :-)
    	} finally {
    		return this;
    	}
    };
    </script>

  12. #12
    Expert éminent

    Homme Profil pro
    Inscrit en
    Janvier 2007
    Messages
    13 474
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Secteur : Finance

    Informations forums :
    Inscription : Janvier 2007
    Messages : 13 474
    Par défaut
    Tu utilises visiblement une variable location qui ne correspond pas à son utilisation habituelle (location est un mot réservé en JS).

    Par contre, pour le Hashtable à problème, il faudrait que tu lances
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    alert("tt");
        var HT= new Hashtable();
        HT.put("lm","mm");
        HT.put("6",6);
        HT.put("7",7);
        var h=HT.get("lm");
      alert(h);
    via le onload du <body> (en en faisant d'abord une fonction) ...

    A+

  13. #13
    Membre Expert Avatar de DoubleU
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    1 106
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 1 106
    Par défaut
    Location, c'est un mot réservé au navigateur.

    Essaie de nommer ta variable autrement.

    Edit: grillé cette fois ^^

  14. #14
    Membre éclairé
    Inscrit en
    Avril 2006
    Messages
    254
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 254
    Par défaut
    j'ai changé le code comme suit:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    <html>
    <head>
     <title>Essaye d'appel des fichiers js</title>
     
      <script language='JavaScript' src='js/Hashtable.js' ></script>
    <!--
     
    function Chargement() {
    	alert('BBB');
    }
     
    </head>
    <body onLoad="Chargement();" >
     <script>
    alert("tt");
        var HT= new Hashtable();
        HT.put("lm","mm");
        HT.put("6",6);
        HT.put("7",7);
        var h=HT.get("lm");
      alert(h);
     </script>
     
     </body>
    </html>
    et j'ai eu les erreurs suivants:

    à la ligne 15
    et:
    l'autre erreur, Hashtable is not defined

  15. #15
    Membre éclairé
    Inscrit en
    Avril 2006
    Messages
    254
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 254
    Par défaut
    Enfin j'ai fini par rézoudre le problème, il fallait tout simplement enlever les balises <script></script> du fichier Hashtable.js.

    Merci encore une fois DoubleU.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 26
    Dernier message: 15/06/2009, 17h02
  2. lancer script a partir d une page html
    Par nita2006 dans le forum Débuter
    Réponses: 7
    Dernier message: 20/06/2008, 23h09
  3. Réponses: 5
    Dernier message: 17/05/2008, 13h15
  4. [Système] Exécuter un script CGI à partir d'une page en PHP
    Par Olivier Regnier dans le forum Langage
    Réponses: 2
    Dernier message: 23/01/2008, 18h08
  5. [HTML] Appel d'un programme en C à partir d'une page HTML
    Par paihpaihtte dans le forum Balisage (X)HTML et validation W3C
    Réponses: 2
    Dernier message: 10/04/2007, 22h33

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo