如何运行我用 JavaScript 编写的代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31887065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:31:06  来源:igfitidea点击:

How do I run code I have written in JavaScript?

javascript

提问by user2607534

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.

我是初学者,我所做的只是在 Codecademy 中练习编写代码。在 google 上广泛搜索如何运行 .js 文件后,我一无所获。我假设我问错了问题,我确信这很简单,但我找不到任何东西。

回答by DhruvJoshi

  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

  1. 打开编辑器。最简单的一种是记事本
  2. 在那里编写基本的 HTML,如下所示

    <html>
        <head>
        </head>
        <body>
            Hello World!
        </body>
    </html>
    
  3. 添加一个脚本标签并将你的 js 写在里面,如下所示

    <html>
        <head>
            <script> 
                alert("hello");
            </script>
        </head>
        <body>
            Hello World!
        </body>
    </html>
    
  4. 或者你可以在一个文件中编写你的 js 代码并保存为 .js 文件并在上面的代码中链接它

    <html>
        <head>
            <script src="myScript.js"></script>
        </head>
        <body>
            Hello World!
        </body>
    </html>
    
  5. 将其另存为 yourfile.HTML 并在任何浏览器中打开

Here's a link to learn more: http://www.w3schools.com/js/js_whereto.asp.

这是了解更多信息的链接:http: //www.w3schools.com/js/js_whereto.asp

回答by luther

Run javascript in your browser simply use this methods:

在浏览器中运行 javascript 只需使用以下方法:

1. use jsfiddle.net

1. 使用jsfiddle.net

2. use developer console your browser (how to open console in Chrome/Firefox/Safari you can read in Wiki)

2. 使用开发者控制台您的浏览器(如何在 Chrome/Firefox/Safari 中打开控制台,您可以在 Wiki 中阅读)

3. write your own file with extention .html and put it:

3. 使用扩展名 .html 编写您自己的文件并将其放入:

<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.

当您发现 JS 时,每种方法都有自己的好处。我们开发人员每天都在使用所有这些方法。

回答by Nabil Kadimi

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:

如果您想像在 Codecademy 中那样练习简单的 JavaScript 指令和代码片段,您可以使用:

回答by Randula Koralage

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.

首先在你的机器上安装和设置 Nodejs。然后编写您的javascript并将其保存在一个文件夹中。打开文件夹内的命令提示符并编写要执行的命令。

node a.js

回答by Nathu

When running a .js file, you just need to add it in your web page. Example.

运行 .js 文件时,您只需要将其添加到您的网页中。例子。

If you have this as the content of a .js file say hello.js

如果您将此作为 .js 文件的内容,请说 hello.js

alert('Yow!');

to use it, you create an HTML file

要使用它,您需要创建一个 HTML 文件

<html>
    <body>
        <script src="hello.js"></script>
    </body>
</html>

回答by Mr.Web

You are to create a file with extension .html, so open up notepad or similar program and write the following:

您将创建一个扩展名为 .html 的文件,因此打开记事本或类似程序并编写以下内容:

<!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.

您的 javascript 操作和代码位于<script>标签内。