Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript runtime error: '$' is undefined

So classic problem, but having a horrible time on finding the actual cause. Typically when I see this error it's because the jQuery reference is after code requiring it, or back jQuery link, or jQuery conflict, etc... so far none of those appear to be the case. Unfortunately seeking out the solution to this problem has lead me to post after post of such cases. I'm sure my problem here is equally as simple, but over an hour of hunting, still no luck...

Edit: Additional information... The solution file (which I've recreated multiple times trying to figure this out. Is a JavaScript Windows Store Blank App template and I'm doing this in Visual studio. The only references files is Windows Library for javascript 1.0, I have tried deleting this to test as well.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>HTML5 Canvas Template</title>
        <style>
            /* styles here */
        </style>
    </head>
    <body>
        <canvas id="myCanvas" width="500" height="500">
            <p>Canvas not supported.</p>
        </canvas>

        <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                var canvas = $("#myCanvas").get(0);
                var context = canvas.getContext("2d");

                function renderContent()
                {
                    // we'll do our drawing here...
                }

                renderContent();
            });
        </script>
</body>
</html>
like image 988
Eric J Fisher Avatar asked Feb 20 '13 02:02

Eric J Fisher


People also ask

Is undefined JavaScript error?

Undefined means that a variable has been declared but has not been assigned a value. In JavaScript, properties and functions can only belong to objects. Since undefined is not an object type, calling a function or a property on such a variable causes the TypeError: Cannot read property of undefined .

What is a JavaScript runtime error?

Runtime Error: A runtime error is an error that occurs during the running of the program, also known as the exceptions. In the example that is given below the syntax is correct, but at runtime, it is trying to call a method that does not exist. Example: <script type= "text/javascript" > // An runtime error here.

What causes runtime errors in JavaScript?

Runtime errors occur when the interpreter comes along some code that it cannot understand. Both loading and syntax errors are simple but can result in unexpected halting of script.

Is undefined in jQuery?

The jQuery undefined is a primitive value that specifies that a variable has not been initialized a value or may not be declared as well. The jQuery undefined is a built-in primitive data type in jQuery.


2 Answers

It's states that JQuery referred URL is not correct

Try this:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
like image 116
coder Avatar answered Sep 28 '22 07:09

coder


I tried everything listed above and nothing seems to work until I put this string

<script src="../scripts/jquery-2.2.3.min.js" type="text/javascript"></script>

in the head section of the HTML file. So here's how it looks:

<!DOCTYPE html>
<html>
<head>

    <!-- jQuery Reference -->
    <script src="../scripts/jquery-2.2.3.min.js" type="text/javascript"></script>

    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />


    <title>some title</title>

</head>
<body>
...

And the js file is located at a level below in the folder 'scripts'. Finally, the error is gone and what a relief!

like image 35
RickInLosAngeles Avatar answered Sep 28 '22 09:09

RickInLosAngeles