-1

Why does is say - '_' is undefined, below is a basic simple example and a fiddle. thank you

http://jsfiddle.net/fuEfq/

-------------index.html---------------------------
<html>
<head>

    <title></title>

        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="underscore.js"></script>     
        <script type="text/javascript" src="myJs.js"></script>   

    <head>  

    <html>

    <body>


    <script>

    <button type="button" onclick = "loadThis();">Click Me!</button>



    </script>

    </body>

</html>

-------------myJs.js---------------------------


var loadThis = function(){

    vas x = _.uniq([1, 2, 1, 3, 1, 4]);

    alert(x);

};
Hello-World
  • 9,277
  • 23
  • 88
  • 154
  • 1
    The example code in the JS Fiddle does not give the same error as you describe in the question. Please provide an example that accurately reflects your problem. – Quentin Apr 09 '13 at 09:05
  • Your fiddle cannot find `loadThis` because you've selected the option to load JavaScript in an onLoad handler and it's defined as local variable. If you fix that, it works as expected. The obvious suspect is that `underscore.js` is not loading. All modern browsers have tools to verify that. – Álvaro González Apr 09 '13 at 09:09
  • Are you sure the `underscore.js` script was loaded correctly from you page? Check the error console. – Bergi Apr 09 '13 at 09:17
  • See also the possible duplicate [JavaScript not running on jsfiddle.net](http://stackoverflow.com/questions/5468350/javascript-not-running-on-jsfiddle-net) if your question is just about the fiddle – Bergi Apr 09 '13 at 09:20

1 Answers1

4

The problem with your fiddle is that loadThis is contained within a function that jsFiddle provides for you (wrapping your code), and the error is that loadThis, not _, is undefined. (If you look on the left, you'll see you have "onLoad" in the second drop-down selected. This means that jsFiddle generates a window.load callback for you and puts your code in that callback.) If I fix that with this updated fiddle, it works fine.

If you're seeing an error in your real code saying _ is undefined, that suggests you haven't (successfully) loaded underscore.js. This would typically be because you didn't put the script tag for it in, the script tag was misformed in some way, or the URL in the tag was wrong. If you look in the JavaScript console of your browser (if your browser doesn't have one, switch to a modern browser, which will), which will show you if the URL is wrong.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875