Maciej Pszczolinski
Maciej Pszczolinski

Reputation: 1958

js file not loaded but next script executed?

I have a very simple page with static JS lib references.

<!DOCTYPE html>
<html>
    <head>
        <title>some title here</title>
        <link rel="stylesheet" type="text/css" href="//SOMESERVER.com/1.0.4815/css/public.css" />
        <!--[if lt IE 9]>
            <link rel="stylesheet" type="text/css" href="//SOMESERVER.com/1.0.4815/css/publicIE8.css" />
            <script src="//SOMESERVER.com/1.0.4614/js/json2.js"></script>
        <![endif]-->
        <script type="text/javascript">
        window.qbaka || (function(a,c){a.__qbaka_eh=a.onerror;a.__qbaka_reports=[];a.onerror=function(){a.__qbaka_reports.push(arguments);if(a.__qbaka_eh)try{a.__qbaka_eh.apply(a,arguments)}catch(b){}};a.onerror.qbaka=1;a.qbaka={report:function(){a.__qbaka_reports...
        </script>

        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javascript" src="//SOMESERVER.com/1.0.4815/js/knockout.js"></script>
        <script type="text/javascript" src="//SOMESERVER.com/1.0.4815/js/colorbox.min.js"></script>
        <script type="text/javascript" src="//SOMESERVER.com/1.0.4815/js/public.js"></script>

        <script type="text/javascript">
            var user = {};
            user.loggedIn = false;
            user.displayName = '';
            user.dateFormat = '';
            user.timeFormat = '';
            // bit more very simple code here
        </script>

        <script type="text/javascript">
            $(document).ready(function () {
                //correct code here
            });
        </script>

Code works perefectly for me and for QA team. But since I use QBAKA for logging JS errors - I got around 50 errors (different browsers, IPs, timezones, datetimes) that tells me:

Unknown variable or function '$'

in line with

$(document).ready(function () {

I always thought that libraries executes in order of appearance, so if I include reference to jQuery, I am sure that I can access immediately - without any try-catch or other stuff.

How it is possible? jQuery library (from google server) is not available and this JS lib is not loaded and code executes further? Any ideas?

Upvotes: 1

Views: 1028

Answers (3)

Satish Sharma
Satish Sharma

Reputation: 9635

try this

<script type="text/javascript">
            $(document).ready(function () {
                //correct code here
            });
        </script>

instead of

<script type="text/javascript">
            $(document).ready(function () {
                //correct code here
            }
        </script>

you have not call your $(document).ready function properly.

Upvotes: 1

curiousguy
curiousguy

Reputation: 3262

That error can caused by :

Your jquery file is not being properly loaded into your page. or

 <script type="text/javascript">
        $(document).ready(function () {

            )};
        </script>

is executing earlier than jquery file is loaded.

For example try this

<script src="https://c15123524.ssl.cf2.rackcdn.com/jquery.min.js"></script>

instead of

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

and check.

Check by view source if the jquery is loading properly or not .

Try to download the jquery file and use it from your local server.

Upvotes: 0

SivaRajini
SivaRajini

Reputation: 7375

if you using some other custom plugin in your page so it may be conflicts with $ (i.e Jquery)

try $.noConflict()

jQuery.noConflict();
(function( $ ) {
  $(function() {
    // More code using $ as alias to jQuery
  });
})(jQuery);

links :

http://api.jquery.com/jquery.noconflict/

http://learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/

Thanks,

Siva

Upvotes: 1

Related Questions