js code"; 2. reference method, the syntax format ""."/> js code"; 2. reference method, the syntax format "".">

Home  >  Article  >  Web Front-end  >  There are several ways to add javascript to a web page

There are several ways to add javascript to a web page

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-07-16 11:16:198080browse

There are two ways to add js to a web page, namely: 1. Direct method, the syntax format is "3f1c4e4b6b16bbbd69b2ee476dc4f83ajs code2cacc6d41bbb37262a98f745aa00fbf0"; 2. Reference method, the syntax format is "c5fcb4d2f4bb65860e720734845c0b182cacc6d41bbb37262a98f745aa00fbf0".

There are several ways to add javascript to a web page

1. Direct method

This is the most commonly used method. Most web pages containing Javascript use this method, such as:

<script type="application/javascript">
  <!--
  document.write("这是Javascript!采用直接插入的方法!");
  //-Javascript结束-->
  </script>

In this example, we can see a new tag: 3f1c4e4b6b16bbbd69b2ee476dc4f83a...2cacc6d41bbb37262a98f745aa00fbf0, and ee7daa49a56abfe5e8409b7b65ffb3c0 is used to tell the browser that this is Programs written in Javascript need to mobilize the corresponding interpreter for interpretation. (w3c has recommended the use of a new standard: 3d1df40a5050db9df35681b82c2528de)

HTML comment tags 64319fc22de3df1e5972a1902b476065: used to remove text that the browser cannot Identified Javascript source code, which is useful for browsers that do not support the Javascript language.

Note that when inserting script tags in non-xhtml documents, if you are not referencing external files, you should add a cdata statement in the script to avoid browser parsing errors caused by the greater than and less than operators

//-End of Javascript: Double slashes indicate the comment part of Javascript, that is, characters from the beginning of // to the end of the line are ignored. As for the documents used in the program. The write() function means to output the text in the brackets to the window, which will be introduced in detail later. Another point to note is that the position of 3f1c4e4b6b16bbbd69b2ee476dc4f83a...2cacc6d41bbb37262a98f745aa00fbf0 is not fixed and can be included in 93f0f5c25f18dab9d176bd4f6de5d30e...9c3bca370b5104690d9ef395f2c5f8d1 or 6c04bd5ca3fcae76e30b72ad730ca86d.. ...36cc49f0c466276486e50c850b7e4956 anywhere.

There is also a more advanced version of the embedded script, which uses the CDATA syntax in HTML (Character Data, which treats all the text in CDATA as plain text, unless it encounters the end of CDATA)

<script language="javascript" type="text/javascript"><!--//--><!CDATA[[//><!--
  //javascript代码
  //--><!]]></script>

2. Reference method

If there is already a Javascript source file (usually with js extension), you can use this reference method to improve the utilization of the program code. The basic format is as follows:

<script src=“url” type="text/javascript"></script>

The Url is the address of the program file. Likewise, such statements can be placed in the head or any part of the body of an HTML document. If you want to achieve the effect of the example in the "Direct Insertion Method", you can first create a Javascript source code file "Script.js" with the following content:

document.write("这是Javascript!采用直接插入的方法!");

In the web page, you can call the program like this: cee21f9efcac218e9a5f8fef8d219abd2cacc6d41bbb37262a98f745aa00fbf0 .

You can also specify the javascript version when importing the file, for example: 3484e333c17cd0f94b343baeeb1742d82cacc6d41bbb37262a98f745aa00fbf0

Note: Any content in the script tag that specifies the src attribute will be ignored.

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of There are several ways to add javascript to a web page. 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