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 :

Variable undefined alors qu'elle existe.


Sujet :

JavaScript

Vue hybride

Message pr�c�dent Message pr�c�dent   Message suivant Message suivant
  1. #1
    Membre �clair�
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    551
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 551
    Par d�faut Variable undefined alors qu'elle existe.
    Salut,

    Je ne comprends pas, j'ai une erreur javascript au niveau de la ligne:
    photos[num_chapter].src = photos_url[num_chapter] ;

    Et cela me dit:
    photos[num_chapter] is undefined.

    Pourtant la variable photos existe bien, regardez le code:
    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
     
    <script language="JavaScript">
    var photos_url = new Array() ;
    var photos = new Array(); 
    photos_url[0] = "http://localhost:8888/Works/ElizaNet/0.jpg" ;
    photos[0] = new Image(); 
    photos_url[1] = "http://localhost:8888/Works/ElizaNet/1.jpg" ;
    photos[1] = new Image(); 
    photos_url[2] = "http://localhost:8888/Works/ElizaNet/2.jpg" ;
    photos[2] = new Image(); 
    photos_url[3] = "http://localhost:8888/Works/ElizaNet/3.jpg" ;
    photos[3] = new Image(); 
    photos_url[4] = "http://localhost:8888/Works/ElizaNet/4.jpg" ;
    photos[4] = new Image(); 
    photos_url[5] = "http://localhost:8888/Works/ElizaNet/5.jpg" ;
    photos[5] = new Image(); 
    function JS_change_chapter(num_chapter) {
      photos[num_chapter].src = photos_url[num_chapter] ;
      //document.getElementById('img_chapter').src = 'http://localhost:8888/Works/ElizaNet/imgs/picbooks/1/1.jpg' ;
      document.getElementById('img_chapter').src = photos[num_chapter] ;
    }
    Vous auriez une id�e ? La je bloque depuis longtemps sur cette erreur...

    Merci,
    Vincent.

  2. #2
    Membre Expert Avatar de franculo_caoulene
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    2 880
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 2 880
    Par d�faut
    Salut,

    J'ai test�, je n'ai aucune erreur :
    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
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <title>test</title>
      <meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
      <style type="text/css">
      * {margin:0;padding:0;}
      body {margin:10px;background:#000;color:#ddd;font-size:1em;font-family:Helvetica, Arial, sans-serif;}
      </style>
    </head>
    <body>
      <img id="i" src="" alt="image" />
      <script type="text/javascript">
      <!--
        var photos_url = new Array() ;
    var photos = new Array(); 
    photos_url[0] = "http://localhost:8888/Works/ElizaNet/0.jpg" ;
    photos[0] = new Image(); 
    photos_url[1] = "http://localhost:8888/Works/ElizaNet/1.jpg" ;
    photos[1] = new Image(); 
    photos_url[2] = "http://localhost:8888/Works/ElizaNet/2.jpg" ;
    photos[2] = new Image(); 
    photos_url[3] = "http://localhost:8888/Works/ElizaNet/3.jpg" ;
    photos[3] = new Image(); 
    photos_url[4] = "http://localhost:8888/Works/ElizaNet/4.jpg" ;
    photos[4] = new Image(); 
    photos_url[5] = "http://localhost:8888/Works/ElizaNet/5.jpg" ;
    photos[5] = new Image(); 
    function JS_change_chapter(num_chapter) {
      photos[num_chapter].src = photos_url[num_chapter] ;
      document.getElementById("i").src = photos[num_chapter] ;
    };
    JS_change_chapter(5);
      //-->
      </script>
    </body>
    </html>

  3. #3
    Membre Expert
    Avatar de emmanuel.remy
    Inscrit en
    Novembre 2005
    Messages
    2 855
    D�tails du profil
    Informations personnelles :
    �ge : 57

    Informations forums :
    Inscription : Novembre 2005
    Messages : 2 855
    Par d�faut
    Salut,

    Pourtant la variable photos existe bien
    Mais l'indice envoy� � la fonction est il correct ?

    ERE

  4. #4
    Membre �clair�
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    551
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 551
    Par d�faut
    Ben ouais, c'est l'indice, je suis trop b�te...
    ;-)

  5. #5
    R�dacteur

    Avatar de Bovino
    Homme Profil pro
    D�veloppeur Web
    Inscrit en
    Juin 2008
    Messages
    23 647
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 55
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activit� : D�veloppeur Web
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : Juin 2008
    Messages : 23 647
    Billets dans le blog
    20
    Par d�faut
    Ceci dit, niveau conception, je vois vraiment pas l'int�r�t de ton tableau photos[]
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    var photos_url = new Array() ;
    photos_url[0] = "http://localhost:8888/Works/ElizaNet/0.jpg" ;
    photos_url[1] = "http://localhost:8888/Works/ElizaNet/1.jpg" ;
    photos_url[2] = "http://localhost:8888/Works/ElizaNet/2.jpg" ;
    photos_url[3] = "http://localhost:8888/Works/ElizaNet/3.jpg" ;
    photos_url[4] = "http://localhost:8888/Works/ElizaNet/4.jpg" ;
    photos_url[5] = "http://localhost:8888/Works/ElizaNet/5.jpg" ;
    function JS_change_chapter(num_chapter) {
      document.getElementById('img_chapter').src = photos_url[num_chapter] ;
    }
    Pas de question technique par MP !
    Tout le monde peut participer � developpez.com, vous avez une id�e, contactez-moi !
    Mes formations video2brain : La formation compl�te sur JavaScriptJavaScript et le DOM par la pratiquePHP 5 et MySQL : les fondamentaux
    Mon livre sur jQuery
    Module Firefox / Chrome d'int�gration de JSFiddle et CodePen sur le forum

  6. #6
    R�dacteur/Mod�rateur

    Avatar de SpaceFrog
    Homme Profil pro
    D�veloppeur Web Php Mysql Html Javascript CSS Apache - Int�grateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 659
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 75
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activit� : D�veloppeur Web Php Mysql Html Javascript CSS Apache - Int�grateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 659
    Billets dans le blog
    1
    Par d�faut
    Preload Beef !
    Ma page Developpez - Mon Blog Developpez
    Pr�sident du CCMPTP (Comit� Contre le Mot "Probl�me" dans les Titres de Posts)
    Deux r�gles du succ�s: 1) Ne communiquez jamais � quelqu'un tout votre savoir...
    Votre post est r�solu ? Alors n'oubliez pas le Tag

    Venez sur le Chat de D�veloppez !

  7. #7
    R�dacteur/Mod�rateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Par d�faut
    Pour �viter ce genre d'erreur, il faut d'abord tester l'existence de l'�l�ment ou la longueur du tableau.
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    if(num_chapter<photos_url.length){
    ...}

  8. #8
    R�dacteur

    Avatar de Bovino
    Homme Profil pro
    D�veloppeur Web
    Inscrit en
    Juin 2008
    Messages
    23 647
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 55
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activit� : D�veloppeur Web
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : Juin 2008
    Messages : 23 647
    Billets dans le blog
    20
    Par d�faut
    Citation Envoy� par SpaceFrog Voir le message
    Preload Beef !

    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    function JS_change_chapter(num_chapter) {
      photos[num_chapter].src = photos_url[num_chapter] ;
      //document.getElementById('img_chapter').src = 'http://localhost:8888/Works/ElizaNet/imgs/picbooks/1/1.jpg' ;
      document.getElementById('img_chapter').src = photos[num_chapter] ;
    }
    Effectivement, preloader une image imm�diatement avant de l'afficher est d'une efficacit� redoutable !
    Mais bon, m�me pour un preload, je ne vois pas l'int�r�t du tableau photos :
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    var photos = new Image;
    var photos_url = new Array() ;
    photos_url[0] = "http://localhost:8888/Works/ElizaNet/0.jpg" ;
    photos_url[1] = "http://localhost:8888/Works/ElizaNet/1.jpg" ;
    photos_url[2] = "http://localhost:8888/Works/ElizaNet/2.jpg" ;
    photos_url[3] = "http://localhost:8888/Works/ElizaNet/3.jpg" ;
    photos_url[4] = "http://localhost:8888/Works/ElizaNet/4.jpg" ;
    photos_url[5] = "http://localhost:8888/Works/ElizaNet/5.jpg" ;
    function preload(){
        for(var i=0; i< photos_url.length; i++){
            photos.src = photos_url[i];
        }
    }
    Pas de question technique par MP !
    Tout le monde peut participer � developpez.com, vous avez une id�e, contactez-moi !
    Mes formations video2brain : La formation compl�te sur JavaScriptJavaScript et le DOM par la pratiquePHP 5 et MySQL : les fondamentaux
    Mon livre sur jQuery
    Module Firefox / Chrome d'int�gration de JSFiddle et CodePen sur le forum

+ R�pondre � la discussion
Cette discussion est r�solue.

Discussions similaires

  1. [PDO] Undefined variable: id alors qu'elle existe.
    Par barale61 dans le forum PHP & Base de donn�es
    R�ponses: 4
    Dernier message: 09/04/2015, 18h27
  2. [HQL] "could not resolve property" alors qu'elle existe
    Par Ceubex dans le forum Hibernate
    R�ponses: 7
    Dernier message: 29/01/2015, 10h24
  3. [PHP 5.3] Variable vide alors qu'elle ne devrait pas l'�tre
    Par ciols dans le forum Langage
    R�ponses: 3
    Dernier message: 10/02/2014, 15h35
  4. Undefined alors qu'elle existe ?
    Par Squallynou dans le forum Langage
    R�ponses: 2
    Dernier message: 22/03/2011, 15h22
  5. [MySQL] Table inconnue 'type_matiere_premiere' dans where clause alors qu'elle existe
    Par roy-mustang dans le forum PHP & Base de donn�es
    R�ponses: 2
    Dernier message: 25/04/2009, 17h06

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