jQuery() and $() undefined, but jQuery works fine [duplicate] - javascript

This question already has answers here:
JQuery - $ is not defined
(36 answers)
Jquery '$' is undefined
(5 answers)
Closed 3 years ago.
SOLVED:
I am the biggest dumb. I was so focused on where I intended to load my scripts (at the end of the body tag) that I wasn't paying attention to the head anymore.
Lo and behold I was loading my main.js twice, once in the head and once after jQuery loaded.
Thank you to everyone who tried to help, but ya can't cure my tunnel vision :/
ORIGINAL POST:
I am importing jQuery from Google's CDN, and I am loading it before my own js script, but on line 1 I'm getting a "$ is undefined" error with my $(document).ready() function.
This error occurs whether I use $ or jQuery as the prefix.
I've been searching through forums for the past hour and all the solutions that people seem to have is to ensure that jQuery is loading before my other scripts and that it's not being interfered with by other libraries that might be using $, but I'm not using any other libraries, and I've taken the path straight from Google's site
I've also tried using Microsoft's CDN and jQuery's CDN.
I've checked in Chrome, Firefox, and Edge, all the same.
The network tab in dev tools shows jQuery loading first.
<body>
<aside>
<div id="nav">
<h2>ABOUT US</h2>
<h2 id="donate-button">DONATE NOW</h2>
<h2>CONTACT US</h2>
<h2>PHOTO GALLERY</h2>
</div>
</aside>
<!--SCRIPTS-->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript" src="Resources/JS/main.js"></script>
</body>
// Resources/JS/main.js
$(document).ready(function() {
console.log("READY");
$("#nav h2").css("background-color", "red")
});
All I'm trying to do is get this error to go away so I can actually start writing my script :/
I'm fairly new to JS and jQuery in particular, and I feel like the solution is so simple and I'm just looking in the wrong spots. But I've done a decent amount of searching and I can't find anyone who's solution works for me too.
EDIT:
Here are some screenshots from Chrome Dev Tools that were requested:

When I come across this issue I make sure to wrap all jQuery code inside the following
(function($){
// add your code here
})(jQuery);
Or add this before any jQuery
$ = jQuery.noConflict();
EG.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
(function($){
$(document).ready(function() {
console.log("READY");
$("#nav h2").css("background-color", "red")
});
})(jQuery);
</script>

The general structure of your scripts should look something like this:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="main.js"></script>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
main.js
$(document).ready(function()
{
console.log("READY");
$('body').append("It's working.");
});

Related

jQuery code always returns "not defined" in Sublime

I have my jQuery linked in the html file before the js file, and I've ensured that it is loading properly in the browser window. The jQuery code actually WORKS, so I have no idea why sublime thinks it cant understand the sytax. I've tried just about every suggestion in this post (JQuery - $ is not defined) which there was a ton. Also, i dont currently have sublimeLinter installed, currently using a console that i set up with this (http://www.wikihow.com/Create-a-Javascript-Console-in-Sublime-Text).
This code:
<html>
<head>
<script type="text/javascript" src="jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body> </body>
</html>
with this js:
var $j = jQuery.noConflict();
$j(document).ready(function() {
$j("body").text("Hello world!");
});
leads to this error message:
Add an ignore comment to the top of your document. What is happening is that your linter is not seeing jQuery as it is not part of the current document. This will force your linter to ignore the jQuery keyword while linting.
/*globals $:false, jQuery:false*/

Jquery load anonymous?

Im a little bit confused now. Because I read all other answers but none is like my case.
I manage to always get working my preloading gif using simple jquery load function.
But now even though all my code is the same Im getting this error...
Like if load was not a funcion....
And my code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script src="assets/js/plugins/owl.carousel.js"></script>
<script type="text/javascript">
$(window).load(function() {
$(".loader").fadeOut("slow");
})
</script>
</head>
<body id="body" data-spy="scroll" data-target=".one-page-header" class="demo-lightbox-gallery">
<div class="loader"></div>
What can it be?
This might solve your problem:
Don't use jQuery 3 yet -- go back to 1.11.3 or such. Especially when using a plugin like owl carousel.

Plugging in a jQuery plugin

I'm trying to use the jquery tokeninput plugin, the demos work fine however when I try to implement it I'm hitting a brick wall. Chrome chucks this at me:
Uncaught TypeError: Object [object Object] has no method 'tokenInput'
Below is an excerpt from my <head>, chrome's resource browser shows both jQuery and jquery.tokeninput are loaded fine. No URL issues.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript" src="/media/js/jquery.tokeninput.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#token").tokenInput("/members/api/members/tokeninput_members/?format=json");
});
</script>
And showing that tokeninput has loaded:
Right, bare-bones page worked fine. After digging a while longer I found this buried at the base of the page:
<script src="http://code.jquery.com/jquery.js"></script>
It seems having multiple versions of jQuery loaded is not a good thing to do.
I am not sure if you already solved it or not. But Try this it should work if your sequence of jquery library inclusion is right (which it seems right), also remove one of jquery.min.js, jquery.js.
Then try this
<script type="text/javascript">
// Any valid variable name is fine.
var j = jQuery.noConflict();
j(document).ready(function () {
j("#token").tokenInput("/members/api/members/tokeninput_members/?format=json");
});
</script>
Check this out to understand why you might need this.
http://api.jquery.com/jQuery.noConflict/

How do I include html file inside another html file

I know that there are a lot of answers to this question, but I have trying them all and I could not resolve this problem.
My specific problem is that I have two files, index.html and part1.html, and I need include part1.html into index.html.
I have tried using:
.load() function of jquery.
<!--#include file="part1.shtml" -->
include with html like <!--#include file="part1.html" -->
but this has resolved my problem, so why so I need it? I have a page with a lot of code and in this moment is almost impossible to work. Why don't use a server language? I do, but I work with a framework in R called shiny and believe me server language is not a solution xD.
When I use .load() I get an error XMLHttpRequest cannot load file:///home/... Origin null is not allowed by Access-Control-Allow-Origin.
Thanks for all.
use jquery
<html>
<head>
<script src="jquery.js"></script>
<script>
$(function{
$("#includedContent").load("b.html");
});
</script>
</head>
<body>
<div id="includedContent"></div>
</body>
</html>
.load should be working fine. A quick example of this, say you have the following div:
<div id="contentDiv"></div>
Using jQuery, you can load your separate HTML file into that div:
$(document).ready(function() {
$("#contentDiv").load("part1.html");
});
It's something to do with SOP(same origin policy) recently I had the same issue...so I used this...hope you can do it like this...here's the link : http://jsfiddle.net/JKZ2h/1/
my html code:
<li>About Us</li>
<div class="bdy"></div>
my js code:
$('.about-link').click(function() {
$('.bdy').html('<iframe sandbox="allow-scripts allow-same-origin" style="width:100%;height:435px;" src="http://www.erail.in/?R=12622"><iframe>');
})
or even this works for a static webpage :
this inside any function...works..:
$('.bdy').html('<object style="width:100%;height:435px;"data="http://www.jhsoftech.com/contactus.php">');

Uncaught ReferenceError $ is not defined

I'm very new to JavaScript (just started a few hours ago and trying to get a script working). I went through a few tutorials on W3 and the 'hello world' code works when I paste it directly into my HTML but I'm having a problem with a script (I've had problems with other scripts as well but I am not sure what I'm doing wrong).
I have this code that I want to test in my HTML, I copied the HTML in and it looks the same then I made a file in my static folder called edit.js and copied the JavaScript into it (exactly as shown). It didn't work no errors on the page but when I click it nothing happens. I tried to paste a W3 'hello world' code in and that worked but this script does not.
I tried to inspect the code in Chrome and that's where I see the above error (under the resources tab). I can open the js file using Chrome which makes me think the js file is accessible and pointing correctly but I'm not sure how to get it working. I'm using Jinja2 as my template engine to render the HTML and in my header I have:
<script language="JavaScript" type="text/javascript" src="static/edit.js"></script>
and in my main template (the one that gets rendered on all pages) I have:
<script language="JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
edit.js:
(even putting it within the script tag directly on the page I want to use it on doesn't work)
$('#editvalue').click(function(e){$('#storedvalue').hide();$('#altervalue').show();});
$('#savevalue').click(function(e){
var showNew = $('#changevalue').val();
$('#altervalue').hide();
$('#storedvalue').show();
$('#storedvalue span').text(showNew);
});​
HTML:
(it's embedded in a larger page)
<head>
<script language="JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript" src="static/edit.js"></script>
</head>
... my html code..
<div id="wrapper">
<div id="container">
<div id="storedvalue"><span>Hello</span> [edit]</div>
<div id="altervalue" style="display:none;"><input type="text" name="changevalue" id="changevalue" value="Hello"> [save]</div>
</div>
</div>
I have never been able to successfully run a JavaScript that wasn't on W3 yet. I get the same problem with other scripts even though I see people online saying they work fine for them. Do I need to do anything extra to make this work?
My two questions are:
What am I doing wrong?
Because Javascript seems to just not work when there's a problem, is there a way to get errors or information on what's actually wrong?
I read Uncaught ReferenceError: $ is not defined? and have been trying to figure this out for the last hour and can't see my problem.
First you need to place the jQuery script tag first.
Second, you need to do one of the following things:
Put your code within this function:
$(document).ready(function(){/*CODE HERE*/});
Or like this:
$(function(){
/*CODE HERE*/
});
The DOM needs to be ready before you can use it. Placing your code within anonymous functions that are executed on the ready event of the DOM is how you can do this.
Edit:
$(function(){
$('#editvalue').click(function(e){$('#storedvalue').hide();$('#altervalue').show();});
$('#savevalue').click(function(e){
var showNew = $('#changevalue').val();
$('#altervalue').hide();
$('#storedvalue').show();
$('#storedvalue span').text(showNew);
});​
});
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
Script tag for jQuery should come before your custom javascript.
Follow by edit.js
<script type="text/javascript" src="static/edit.js"></script>
Try removing the language attribute..sometimes work for me. It's obsolete now .. i think
You need to include jquery before you can use it.

Categories