0

I'm trying to load scripts on web page in the following order:

<head>
 ...
<script src="/file1.js"></script>
<script src="/jquery-1.7.2.min.js"></script>
<script>$.holdReady(true);</script>
<script src="/file2.js"></script>

...
</head>

And sometimes I receive an error "ReferenceError: $ is not defined" in the line <script>$.holdReady(true);</script> but file1.js and jquery-1.7.2.min.js are loaded successfully.

What am I doing wrong?

Roland Illig
  • 37,193
  • 10
  • 75
  • 113
Andrei
  • 4,137
  • 3
  • 23
  • 29
  • usually you need to use a selector eg. $(".myclass").holdready(true); – Scary Wombat Nov 11 '13 at 08:40
  • 3
    @user2310289 - that's not correct. It doesn't work that way. – ahren Nov 11 '13 at 08:41
  • 1
    What's in `file1.js`? Anything referencing `$` ? – ahren Nov 11 '13 at 08:42
  • Example on http://api.jquery.com/jQuery.holdReady/ : $.holdReady( true ); – Andrei Nov 11 '13 at 08:42
  • @ahren: there is no references to $ in file1.js – Andrei Nov 11 '13 at 08:43
  • 1
    Any `asnyc` or `defer` attributes on the jQuery `script` tag? – Boldewyn Nov 11 '13 at 08:46
  • 4
    Can you try using 'jQuery' instead of '$'? Also, no 404 in the console? Have you checked the contents of the jQuery file? – foiseworth Nov 11 '13 at 08:46
  • I would guess there is something wrong with jquery file you have if you are 100% sure it is being loaded properly – Gabriel Sadaka Nov 11 '13 at 08:48
  • @foiseworth: when I'm trying to open content of jQuery file I see the following message in firebug network tab: "Reload the page to get source for: https://domain/jquery-1.7.2.min.js". It seems something wrong with jQuery file loading... – Andrei Nov 11 '13 at 08:52
  • @foiseworth: http status code fo jquery file "200 OK" – Andrei Nov 11 '13 at 08:55
  • What happens when you open the jquery file directly via its URL? – foiseworth Nov 11 '13 at 09:09
  • @foiseworth: I see "Content Encoding Error. The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression." – Andrei Nov 11 '13 at 09:11
  • Response headers: Accept-Ranges bytes Access-Control-Allow-Orig... * Content-Encoding gzip Content-Length 94840 Content-Type application/x-javascript Date Mon, 11 Nov 2013 09:13:00 GMT Etag "2f2f7f4048d9ce1:0" Last-Modified Mon, 04 Nov 2013 10:25:58 GMT Vary Accept-Encoding – Andrei Nov 11 '13 at 09:13
  • @Andrei - There's your problem. For some reason, your copy of jQuery is being served incorrectly by the server. Either have a look at the server's settings, or try using a third party CDN (e.g. https://developers.google.com/speed/libraries/devguide#jquery). – Olly Hodgson Nov 11 '13 at 09:26
  • @Andrei good practise to specify – Barun Nov 11 '13 at 09:35
  • 1
    @Barun HTML5 makes the `type="text/javascript"` the default and hence optional (because browsers did so anyway since back to Netscape times) – Boldewyn Nov 11 '13 at 10:45

2 Answers2

0

As @foiseworth says you can try to use jQuery instead of $.

You ca also try something like this:

(function($){
    $.holdReady(true);
})(jQuery);

Try to put the jQuery inclusion right after you open head tag and then holdReady right after jQuery and then your scripts if you can.

Jonathan
  • 718
  • 8
  • 22
0

You can try reordering the imports this way. All imports first then the calling code.

<script src="/jquery-1.7.2.min.js"></script>
<script src="/file1.js"></script>
<script src="/file2.js"></script>
<script>$.holdReady(true);</script>

My answer refers to this question on stackoverflow : uncaught-referenceerror-is-not-defined

Community
  • 1
  • 1
Barun
  • 1,320
  • 2
  • 11
  • 18