how to use js code

Before learning JavaScript We should first look at how to use JavaScript code? Here we take a look at the usage js code.

JavaScript

We are writing JavaScript code, there are two: one is directly js code embedded in an HTML page, the other is a JavaScript file through external links.

Let's look at the first usage: js code embedded directly in HTML pages

If the JavaScript code is embedded directly in the HTML page, we need to use <script> tag, <script> and </ script> JavaScript will tell where to begin and end.

The lines between the <script> and </ script> contains JavaScript:

<script>

alert("My First JavaScript");

</script>

HTML page at any position (preferably the rear head portion of the body portion of the body) can be inserted script tag.

You might see use type = "text / javascript" in the <script> tag, but now do not have to do so. JavaScript is now all modern browsers and HTML5 in the default scripting language.

Let's look at a concrete example

<!DOCTYPE html>

<html>

<head>

</head>

<body>

<script>

document.write("<h1>This is a heading</h1>");

document.write("<p>This is a paragraph</p>");

</script>

</body>

</html>

The code is embedded directly in js code in the HTML page.

Then we take a look js code second usage: JavaScript files through external links.

Sometimes too much JavaScript code, we can place the code in a separate JavaScript file and then link up.

We link to external JavaScript files can have the following steps

1, first we create a JavaScript file, a .js extension

2, then going to write the code written Js js file, and save the file.

3, use the script tag to JavaScript file linked to the HTML file

<Script type = "text / javascript" src = "JavaScript file name and path"> </ script>

It should be noted that it is important, in a script tag already introduced external js file, you can not write Js command in its start and end tags in the.

Let's look at a concrete example

Let's build a js file sample.js

Then write js code

document.write("<p>This is a paragraph</p>");

Finally, use the script tag to JavaScript file links to HTML file

<!DOCTYPE html>

<html>

<head>

</head>

<body>

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

</body>

</html>

These are the two methods used js code

These are the details of how to use js code, please pay attention to more php Chinese net other related articles!

Guess you like

Origin www.cnblogs.com/cc888/p/11635131.html