ReferenceError: document is not defined (JavaScript in VS Code) - javascript

Im learning JS. Found a good video "JavaScript Programming - Full Course" and stucked at the 2nd part where I need to use DOM. I have the HTML document, in i refered to the JS file. Other stuff working well, but this:
let messageEL = document.getElementById("message-el")
Always get this error:
ReferenceError: document is not defined
I installed live server, and it works one time, than stops.
What can I do, to fix this? Do I need to download some extension?
Please tell me the solution as simple as u can Im at the beginning of the learning path.
This is the code and the error

In your screenshot, you're attempting to run the script using Node.js (the server-side JavaScript executable).
Since you say you're running live-server, you should be looking at your browser instead, not trying to run your code via Node.

Related

Jupyter NB prevents Javascript execution from taking effect

I am running a jupyter notebook on a local server and spent the entire last day trying to get a javascript library for charting to run. It's called Dygraph (http://dygraphs.com/). Outside of the jupyter notebook everything is working as it should, but if I want to use the library inside a notebook, then things get messy. Even displaying a simple html file, which uses the library and works just fine outside jupyter, is not working when viewed via the server. All resources are in the right place and seem to get executed, just without having a lasting effect.
I know that the script is executed, because I appended "console.log('okay')" to the local source file (http://dygraphs.com/2.1.0/dygraph.min.js) and "okay" is in fact showing up in the browser console.
I executed the script in the following ways (none of which were effective):
By using IPython to paste a script tag, which links to the resource (src="...")
By reading the content of the script using python and using IPython to execute the javascript directly
The most obvious way: by manually copy-pasting the content of
dygraph.min.js into the console
If I execute 3) on any other site, then I can successfully run the following simple statement:
console.log(Dygraph)
However when I am inside the notebook and try the same, I instead just get:
ReferenceError: Dygraph is not defined
How is this possible and how to fix it?
PS:
I also tried using the python wrapper PyDyGraphs (https://github.com/DayStarEngineering/PyDyGraphs). The example code of the github page throws the same ReferenceError and the graph is not displayed correctly (instead there is just the empty div).

How to compile Javascript console application locally?

I am a beginner in Javascript, I decided to practice Javascript by problem solving using it, I found an online judge that accepts Javascript V8 4.8.0 code.
So, I searched online to get that version of Javascript V8 on my machine, but I couldn't find any easy way, All the pages were explaining how to build it, and it seems to be a process that I don't need to go through.
Is there an easy way to compile and run command line apps written in Javascript on my machine?
Note: I don't want to use node.js because I tried using it's I/O and
as a beginner I think it is complex in some way.
Update: I found that package manager pbox.me which provides a version of V8 JavaScript Engine and I managed to install it.
Yet another problem appeared: whenever I try to run a js file writing d8 myfile.js in command line nothing happens as if it is an empty program, knowing that I tryied to d8.exe file and it is working, and I made sure the PATH is inserted in the environment variables.
What am I doing wrong?
The easiest way to get started with JavaScript is probably to use it in a browser. You can type simple things directly into the browser's JavaScript console (check the menu); or you can embed your code in a simple HTML document.
If you want, you can even pretty easily implement the readline()/print() functions, so you can pretend to be doing stdin/stdout based I/O: just read from an array of strings, and send output to console.log (or create DOM nodes if you want to be fancy and/or learn how to generate dynamic website content by hand).
Side note: V8 4.8 is severely outdated, don't use it to execute code you haven't written yourself.

JavaScript doesn't work on localhost

First, excuse me if my English is not completely right, as I am Dutch.
I'm busy working on a project. Since a short time, I get some errors in my console when I'm checking the website on my localhost. I'm using USB Webserver, but I had the same problem when I was using WAMP.
The strangest thing is, that the website is working on my live server, without errors.
The errors I get in my console on localhost are:
Uncaught SyntaxError: Invalid or unexpected token
Uncaught ReferenceError: jQuery is not defined
I know that, according to the second error, it might be a problem with the order in which the files are placed, but I have done that in the way it was done by the creators of my template.
The only thing I have done, is combining all the JavaScript files and minify them.
When I go to the source code on my live server, there's nothing wrong with the JavaScript code. But when I open the source code on my localhost, I get wrong symbols in the code, that's breaking up the actual code.
One line:
retÀ‚ã����À‚ã��������������������P‡Ê������������`ŸÊ����(ƒã������������à‚ã�����#������à‚ã������������idden"
The charset in my html is set to utf-8. I hope you guys can help me out! :)
If you need more information, just let me know.
Well,the first error in your console is exactly indicating that somewhere in your code has a syntax error.
You said your "html is set to utf-8",you might put the javascript code in the html.Make a try to separate it into a single file.

google app script javascript

I am trying to use the wonderful Daff library by Paul Fitz in a script designed to bring some version control to a google sheet Im working on. However I can't seem to include the javascript file in the script. I've tried importing it as an html file with tags, and with an eval() call on an external source but both have produced either syntax errors or Undefined Exports errors. Any idea what this means? with a little research I see that this means node.js isn't included? Anyway around this or way to include library in the script? Here is external source.
The Answer Provide by Mr. Fritz himself involved deleting the first line of the file "#!/usr/bin/env node" and then wrapping the entire code in an init function to then call from outside!

Processing.js FloatList doesn't work on web

My processing.js sketch uses FloatList and it works when I build it with Sublime. However, when I try to run it on a web page, the sketch doesn't load and I get Uncaught Processing.js: Unable to execute pjs sketch: ReferenceError: FloatList is not defined from the javascript console. Why is this happening and how can I fix it?
Any suggestions and help would be gratefully appreciated.
Well, the error says it all: Processing.js does not have a FloatList class. You can confirm this by checking out the Processing.js reference.
I'm not sure why Processing.js didn't implement the FloatList class. Maybe because JavaScript arrays are so different than Java arrays. That's just a guess though. Here is an open issue about this on Processing.js's GitHub page. Here is another issue that shows which classes have been implemented. FloatList is indeed not yet implemented.
In any case, you're going to have to modify your code to get rid of the FloatList class.
Of course, Processing.js is open source. So you could always add the FloatList class yourself! You might even be able to copy much of the source directly.

Categories

Resources