-1

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:

enter image description here

  • 1
    Welcome to Stack Overflow. Please include a [mcve] representing your current code -- we could make guesses at what's going wrong for you, but without being able to see what you're doing they would just be guesses. – Daniel Beck Jun 14 '17 at 15:07
  • Can you add any sort of code? – Lixus Jun 14 '17 at 15:07
  • 1
    So the jQuery code works but Sublime is displaying an error? If it works, then it shouldn't matter what Sublime says. – APAD1 Jun 14 '17 at 15:09
  • Have you checked to make sure jQuery isn't accidentally being loaded twice? That can throw that error on occasion as well. – dgeare Jun 14 '17 at 15:10
  • @DanielBeck code works but editor doesn't understand, that's OP issue – niceman Jun 14 '17 at 15:10
  • @dgeare no error is thrown in browser's console, it's Sublime which can't understand the code – niceman Jun 14 '17 at 15:10
  • as @APAD1 said if code works it doesn't matter if the editor understands it, pay attention that javascript is dynamically typed so IDEs/editors can't understand everything, if you like you can try vscode , it fetches `@types/jquery`(and `@types/others` as well) to give you help(if it doesn't fetch them automatically it's easy `npm -i --save-dev @types/jquery`, I'm not sure if Sublime supports typescript wrappers – niceman Jun 14 '17 at 15:13
  • 1
    Yeah, I didn't notice that at first @niceman. I updated the question title, meanwhile, Fullmetal7777 can you clarify exactly what error message you're seeing in Sublime? (Sublime has tons of plugins to support linting and highlighting pretty much any format; I don't think it's necessary to suggest switching to a different code editor. I don't agree with the "it doesn't matter" comments -- in-editor linting is useful) – Daniel Beck Jun 14 '17 at 15:13
  • also you can document your functions or code with JSDoc to help the editor if it uses it(vscode uses it) – niceman Jun 14 '17 at 15:14
  • sorry for not being specific enough, first time posting here lol ok heres my code, i deleted everything i didnt need. Cant figure out how to start new line, so it might be messy " " and js "var $j = jQuery.noConflict(); $j(document).ready(function () { $j("body").text("Hello world!"); });" I want to be able to debug is bascially why im trying to fix this. I also had the same error in other programs – Fullmetal7777 Jun 14 '17 at 15:28
  • i get the general error that whatever jquery function comes up first isnt defined. http://prntscr.com/fjq2br if i deleted that line, that it would just say the $ isnt defined in the "$(document).ready(function () {" line – Fullmetal7777 Jun 14 '17 at 15:34
  • Also, as the code shows, ive linked a downloaded jquery file and deleted everything i dont need so its easier to debug. – Fullmetal7777 Jun 14 '17 at 15:39
  • Are you wrapping that script inside a pair of – Korgrue Jun 14 '17 at 16:10
  • I've edited your question text to include the code and screenshot from the comments (as you've noticed, code in comments isn't easy to read; in future you can edit the question itself when you need to make updates.) (@Korgrue that was my fault, I accidentally stuck the js and html together) – Daniel Beck Jun 14 '17 at 16:11
  • (I'm sorry that I don't know the answer to this question-- sublimeLinter doesn't show this message for me but I honestly don't recall whether I had to change its default configuration for that, it's been years since I set it up... You might find something useful in the docs http://www.sublimelinter.com/en/latest/troubleshooting.html (assuming that's the linter you're using...) – Daniel Beck Jun 14 '17 at 16:13
  • @DanielBeck not using sublime Linter unfortunately. I just have the console up (using this http://www.wikihow.com/Create-a-Javascript-Console-in-Sublime-Text), and thats what pops up. Could that be my problem? It was the only way to show sublime js output through console.log – Fullmetal7777 Jun 14 '17 at 16:30
  • Ah! OK, that's a workflow I'm totally unfamiliar with, so I'm even less able to be of help ¯\_(ツ)_/¯ – Daniel Beck Jun 14 '17 at 16:36

1 Answers1

1

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*/
Korgrue
  • 3,430
  • 1
  • 13
  • 20
  • As i recently sent to danielBeck, im using a console (which i got through this http://www.wikihow.com/Create-a-Javascript-Console-in-Sublime-Text) to show js output. Could the problem be that the console i set up cant interpret jQuery output as well? It was the only way i could figure out how to show js console.log output so that i knew everything was behaving the way it should – Fullmetal7777 Jun 14 '17 at 16:36
  • Have you checked the browser console to see if the same error is occurring there? If not, then yes - the console itself is likely the issue. – Korgrue Jun 14 '17 at 16:39
  • The browser console runs the code and it works as expected. Well that sucks lol so how should i go about debugging jQuery code, just have to visually look through it and debug that way? Also, is there a way to still be able to console.log js code in sublime without doing it the way i did? – Fullmetal7777 Jun 14 '17 at 16:43
  • I always debug JS in the browser console, that way If the client is introducing a bug I will catch it. – Korgrue Jun 14 '17 at 16:44