2012-01-30 4 views
0

Je veux essayer une lib ala jquery mais je ne comprends pas pourquoi ma méthode getUrl ne produit rien, n'est-ce pas?javascript fonction anonyme: pourquoi mon script ne fonctionne pas

mylib.js

(function() { 

    var scripts = document.getElementsByTagName('script'); 
    var index = scripts.length - 1; 
    var thisScript = scripts[index]; 

    var myLib = {  
     getUrl: function() { 
      return thisScript; 
     }    
    } 
} 

if (!window.$$) {window.$$ = myLib} 

)(); 

et à mytest.html

<html> 
<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.js"></script> 
    <script src="mylib.js" type=text/javascript></script> 
</head> 

<body> 
    <DIV id=url>url</DIV> 
    <script> 
    var url = $$.getUrl(); 
    jQuery("#url").html(url); 
    </script> 
</body> 
</html> 

Répondre

5

Votre code avait un } mal placé.

(function() { 
    var scripts = document.getElementsByTagName('script'); 
    var index = scripts.length - 1; 
    var thisScript = scripts[index]; 

    var myLib = {  
     getUrl: function() { 
      return thisScript; 
     }    
    }; 
//} <-- Remove this. 

    if (!window.$$) {window.$$ = myLib;} 
} // <-- Add this, the closing brace of the anonymous function 
)(); 
+0

merci je trouve difficile de trouver des erreurs :) – user310291

Questions connexes