简体   繁体   中英

How do I run code I have written in JavaScript?

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.

  1. open an editor. Simplest one is notepad
  2. Write basic HTML there like below

     <html> <head> </head> <body> Hello World! </body> </html> 
  3. Add a script tag and write your js inside it like below

     <html> <head> <script> alert("hello"); </script> </head> <body> Hello World! </body> </html> 
  4. 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> 
  5. 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 .

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.

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:

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM