I am a beginner, all I have done is practiced writing code in Codecademy. After extensive searches of google for how to run a .js file, I turned up nothing. I assume I am asking the wrong question, and I am sure it is simple, but I couldn't find anything.
6 Answers
- open an editor. Simplest one is notepad
Write basic HTML there like below
<html> <head> </head> <body> Hello World! </body> </html>
Add a script tag and write your js inside it like below
<html> <head> <script> alert("hello"); </script> </head> <body> Hello World! </body> </html>
or you can write your js code in a file and save as .js file and link that in the above code
<html> <head> <script src="myScript.js"></script> </head> <body> Hello World! </body> </html>
Save this as yourfile.HTML and open in any browser
Here's a link to learn more: http://www.w3schools.com/js/js_whereto.asp.
-
Thank you for the answer. I am still confused though. When I tried to use the code from the fourth step in your answer, all it shows is "Hello World!" although myScript.js had other things. What am I missing? Aug 7, 2015 at 22:06
-
@user2607534 I would request you to post the contents of your js file in the question. Or maybe separate question. As a tip, i can tell you this-> If you are using Chrome,on your browser right click and choose
Inspect Element
. This should open dev toolbar. Click onconsole
and see if there were any errors in js file. Aug 7, 2015 at 22:10 -
This is an addition to previous answers.
If you want to practice simple JavaScript instructions and code snippets like you did in Codecademy you can use:
- Your browser console, I believe pressing F12 will open it on all popular browsers
- JS Console
- Node.js's REPL (installation instructions)
First install and setup Nodejs in your machine. Then write your javascript and save it in a folder. Open the command prompt inside the folder and write the command to execute.
node a.js
Run javascript in your browser simply use this methods:
1. use jsfiddle.net
2. use developer console your browser (how to open console in Chrome/Firefox/Safari you can read in Wiki)
3. write your own file with extention .html and put it:
<script>
alert('Hello world!');
</script>
into the file, save file and open on browser.
Every method have own benefits when you discover JS. We developers use every day all of this methods.
You are to create a file with extension .html, so open up notepad or similar program and write the following:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
//Your code
alert("You made your fist javascript!");
</script>
</body>
</html>
Your javascript actions and codes go inside the <script>
tag.
When running a .js file, you just need to add it in your web page. Example.
If you have this as the content of a .js file say hello.js
alert('Yow!');
to use it, you create an HTML file
<html>
<body>
<script src="hello.js"></script>
</body>
</html>
Shift + F8
<script>
in a html page by your browser, or you can use node.js to run a script from the command line