1uncaught reference error: $ is not defined - javascript

Why do I get this error on 2 of my externally called JavaScript files?
I tried the code inside my index.html file and it ran perfectly.
I've called all my scripts before stylesheets.
There are 4 scripts running on this page, all JavaScript, 2 of them work but 2 of them don't.

Try putting jQuery script tag on top of the other script tags.
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>

You will receive this error if you are trying to use jQuery without having included the library.
Did you forget to include the jQuery library? Or maybe your url to the .js file is incorrect.

Related

Difference between linking a javascript file and directly adding script into html

I am recreating a javascript game which requires tables, but when I link my javascript file to my index.html the grid does not show on my browser, but when I directly add my javascript inside the script tag in my html, the grid shows on the browser.
Any help would be appreciated thank you!
This is my code by referencing a js file inside html(the tables do not show)
html with js link
This is my code without referencing a js file(tables do show)
html without js link
This is my main.js
main js
This is the browser without linking js file
browser without js file
This is my browser with linking js file
browser with js reference
First I linked the js file with script src=js path but it did not work but it worked when I put the javascript directly inside the html script tag. I was wondering how I can make it work with referencing a separate js file for a cleaner html code.
It's due to that your local paths are not resolving correctly. Without seeing directory structure I can't know what could be wrong.
First of all, please re-check and verify that both files main.js and index.html are in the same directory.
Alternatively, you could create folder called js inside of your main directory (where index.html is) and then move the main.js into that folder and try:
<script type="text/javascript" src="/js/main.js"></script>
index.html and main.js files must be at the same directory in order to include .js file as you did
<script type="text/javascript" src="main.js"></script>
UPDATE (I will leave answer whole, if somebody also gets stuck, so they can troubleshoot the problem):
The reason why it couldn't resolve path is because there was blank spaces between src and = ; when giving src, that part needs to be connected.
Just like this
<script type="text/javascript" src="main.js"></script>
Take a look here:
What happens:

Uncaught TypeError: $(...).modal is not a function at HTMLButtonElement.<anonymous>

this is my quote of jquery:
<script src="../js/jquery-1.11.1.min.js"></script>
I have searcher for two hours, this project can work in other pc. In my pc ,it just can't... help!
modal() is not a standard method of jQuery.
You will need to include another library that has a modal() method - such as jQueryUI, or Bootstrap.
If you have included one of those libraries you need to ensure that the URL to the file is correct, and that you have included it after jquery.js.
You already include the bootstrap.main.js but still you have a problem, then, there may be issues of .JS file sequence as the query is the core library file so it is always included a first and other plugins works * (which are depended on jQuery) *
So, My suggestion is to rearrange your .JS files and try once again.

How to get the coffeescript working in Play framework 2.3.1?

I am following the "Using Play Framework with scala" tutorial. I am able to follow all the steps except the last one to use the coffeescript with jquery. I can see the javascript file getting generated, but in the browser, I am seeing this error
"ReferenceError: $ is not defined".
I am new to javascript and coffeescript,
here is my coffeescript code:
and here is the javascript as shown in the browser console
is there some syntax issue that can cause the problem? Help appreciated.
I am attaching the image, if indentation could be one of the reasons for this to fail.
add this line (depending on your version of jQuery)
<script src="#routes.Assets.at("javascripts/jquery-1.11.2.js")" type="text/javascript"></script>
to the <head> </head> section in app/views/main.scala.html .
For me, this template is loading for every page. but first you need to download jQuery and add it to your javascripts folder (under public).
In Play 2.3: Note the lib/jquery/jquery.js path. The lib folder denotes the extract WebJar assets, the jquery folder corresponds to the WebJar artifactId, and the jquery.js refers to the required asset at the root of the WebJar.
So just add
<script type="text/javascript" src="#routes.Assets.versioned("lib/jquery/jquery.js")"></script>
to the <head> </head> section in app/views/main.scala.html.
basic javascript, now everything seems crystal clear.
Just one line to include jquery in the index.scala.html to include jquery plugin.

using Jquery in my .js file

I am very new to web development and Javascript in general and I hae done some Jquery coding by adding my code in my HTMl code all the time. Now, I just moved the same code to my .Js file and my console shows me following error:
$ is not defined [Break On This Error] $(document).ready(function () {
It just says JQuery is not defined. It means that I can not refer to Jquery or any other Javascript files in my Js file?
Or there is something that I am missing here?
Just include jQuery before your script is called
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
For other jQuery stuff available in CDN, see this:
http://docs.jquery.com/Downloading_jQuery#CDN_Hosted_jQuery
Update: This is as close to "using" as you can get, in my mind. It's not required to download the jQuery library to your system or host it on your server.
in your html did you move the whole jquery file into your .js file? you shoudl have it as something like:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"type="text/javascript"></script>
// Now load your .js file
<script type="text/javascript" src="your .js file"></script>
Also if you downloaded jquery forget about it, the top link means you're loading jquery from google so you'll always have the latest version, I really cant be much more help unless you upload your code, sorry.

JavaScript function not found in JSP

I have a few JSP pages, that include some JavaScript(jquery, jquery mobile and some javascript functions that I wrote).
When loading the pages and try to run my functions, I get in Firebug an error, that the function was not found. I have looked into the page source, and the function is there.
All the other jquery mobile functions work.
The only way to make my script work is to make a forced refresh(ctrl+f5).
Why is this happening? How can I fix it?
EDIT
It seems that a simple refresh would also work.
Here is the source code of the page:
http://pastebin.com/6sJnfPDQ
I have retagged your question to remove "Java" and "JSP", as this is irrelevant (server vs browser).
Once your JSP is rendered in the browser, please do look in the page source and see what happened to your tags.
make sure all of your js files are being loaded properly.
also make sure that your js files are being loaded in the proper order.
make sure that, where necessary, you're wrapping your JS in a document ready function of some type
also, I recommend that you add the type attribute to your script tags:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>

Categories