Home  >  Article  >  Web Front-end  >  Can JavaScript run alone?

Can JavaScript run alone?

青灯夜游
青灯夜游Original
2022-09-29 17:48:403852browse

JavaScript cannot be run alone. JavaScript is a scripting language that cannot be run independently like an exe program and can only be executed in the host environment. Two running methods: 1. Use the script tag to put the JavaScript code in HTML and run it with the help of the browser environment; 2. Run the JavaScript code with the help of the node.js environment.

Can JavaScript run alone?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

JavaScript is a cross-platform interpreted, dynamically typed, weakly typed, prototype-based scripting language. It cannot be run independently like an exe program and can only be executed in the host environment.

Running method 1: You need to use the script tag to put it in HTML and run it with the help of the browser environment.

Embedding JavaScript scripts in HTML pages requires the use of the <script> tag. Users can directly write JavaScript code in the <script> tag</script>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>JavaScript程序</title>
    <script type="text/javascript">
        document.write("<h1>PHP中文网:https://www.php.cn/</h1>");
    </script>
</head>
<body></body>
</html>

Can JavaScript run alone?

You can also put JavaScript code in a JavaScript script file. JavaScript script files are text files with the extension .js and can be edited using any text editor. JavaScript files can then be imported into HTML pages using the <script> tag. </script>

Example:

Write the following JavaScript code in the test.js file.

alert("PHP中文网:https://www.php.cn/");

Insert a <script> tag within the <head> tag of the HTML document. Define the src attribute and set the attribute value to a URL string pointing to an external JavaScript file. The code is as follows: <br/></script>

<script type="text/javascript" src="test.js"></script>

Can JavaScript run alone?

Note: When using the <script> tag to include external JavaScript files, the default file type is Javascript. Therefore, regardless of whether the loaded file has a .js extension or not, the browser will parse it as a JavaScript script. </script>

Running method 2: You can run it with the help of node.js.

1. Create and write a JavaScript script file

Can JavaScript run alone?

2. Open the cmd command window

Can JavaScript run alone?

3. Directly execute the "node js file name specific path" command to execute the js script file

My js file is in the D:\nodejs\demo directory, so execute

node D:\nodejs\demo\hello.js

Can JavaScript run alone?

4. Or, first use the "cd directory path" command to locate the directory where the js file is located, and then execute: node fileName command ( fileName file name), you can execute the corresponding js script file. Example:

node hello.js

Can JavaScript run alone?

Description:

For the js file following node, .jsThe suffix can be added or not

Note:

It is best not to use node.js and Chinese in the file name, and there should be no spaces in the file name

More node related knowledge , please visit: nodejs tutorial!

The above is the detailed content of Can JavaScript run alone?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn