Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I execute .js files locally in my browser? [closed]

Hello i was wondering how i can type a javascript game on textmate with my mac and have just a regular .js file but than take the .js file and open it and have it run in chrome like if i have it say "Hello World!" than Hello World! would show up in chrome.

An example of what I'm after is in this video: http://vimeo.com/105955605

like image 484
Program Beast Avatar asked Dec 02 '14 04:12

Program Beast


People also ask

How do I run JavaScript locally in my browser?

To execute JavaScript in a browser you have two options — either put it inside a script element anywhere inside an HTML document, or put it inside an external JavaScript file (with a . js extension) and then reference that file inside the HTML document using an empty script element with a src attribute.

Can you run JavaScript outside of a browser?

You can run JavaScript console in terminal or any command-line interface using Node. js, an open-source, platform-agnostic runtime that executes JavaScript outside a web browser.


2 Answers

Around 1:51 in the video, notice how she puts a <script> tag in there? The way it works is like this:

Create an html file (that's just a text file with a .html ending) somewhere on your computer. In the same folder that you put index.html, put a javascript file (that's just a textfile with a .js ending - let's call it game.js). Then, in your index.html file, put some html that includes the script tag with game.js, like Mary did in the video. index.html should look something like this:

<html>     <head>         <script src="game.js"></script>     </head> </html> 

Now, double click on that file in finder, and it should open it up in your browser. To open up the console to see the output of your javascript code, hit Command-alt-j (those three buttons at the same time).

Good luck on your journey, hope it's as fun for you as it has been for me so far :)

like image 130
thataustin Avatar answered Oct 03 '22 02:10

thataustin


If you're using Google Chrome you can use the Chrome Dev Editor: https://github.com/dart-lang/chromedeveditor

like image 26
hypery2k Avatar answered Oct 03 '22 04:10

hypery2k