Home >Web Front-end >Front-end Q&A >How to quote javascript in web pages

How to quote javascript in web pages

PHPz
PHPzOriginal
2023-04-24 14:47:143120browse

With the continuous development of network technology, web pages are becoming more and more interactive. In order to make web pages have better user experience and interactivity, we often use JavaScript scripts to implement various functions. However, to use JavaScript in a web page, we need to reference it in the web page. This article will introduce how web pages reference JavaScript scripts.

1. Write JavaScript code directly in the HTML file

A simple way to reference JavaScript is to write the entire JavaScript code directly in the HTML file. In HTML files, we can use <script> and </script> tags to wrap JavaScript code and add it in

or inside the html tag, as shown below:
nbsp;html>


    <title>网页引用JavaScript示例</title>
    <script>
        function myFunction() {
            alert("请点击按钮!");
        }
    </script>


    <button>点击我</button>

In this example, we can see that the

tag contains a piece of JavaScript code that defines a function named myFunction. We added a button within the tag and bound the function to the button's onclick event, which will be triggered when the user clicks the button. This method is simple and easy to understand, but may not be conducive to maintenance of complex JavaScript code.

2. Reference external JavaScript files in HTML files

Another method is to write the JavaScript code separately into an independent .js file and reference it in the HTML file. The advantage of this method is that JavaScript code and HTML code are independent of each other and easy to maintain. When referencing, we need to use the <script> tag to refer to external JavaScript files. For example, we save the previous JavaScript code to a file called test.js and then reference it in the HTML file using the following code: </script>

nbsp;html>


    <title>网页引用JavaScript示例</title>
    <script></script>


    <button>点击我</button>

In this example, we can see that we added a <script> tag, and specify the URL of the external JavaScript file to be referenced (for example, our test.js file here), so that the page will call the corresponding js file to implement the JavaScript function. </script>

It is worth noting that when the external JavaScript file and the HTML file are located in the same directory, we can use relative paths to specify the location of the js code file, such as:

<script></script>

where ../ means Jump out of the current directory and enter the js folder in the directory above it to find the test.js file.

3. Embed and reference external JavaScript files in HTML

Sometimes for convenience, external JavaScript files are referenced in HTML files and embedded JavaScript is written at the same time. In fact, this is also possible. It can be achieved by specifying the type attribute in the <script> tag, as shown below: </script>

nbsp;html>


    网页引用JavaScript示例
    <script></script>
    <script>
        function myFunction() {
            alert("请点击按钮!");
        }
    </script>


    

In this example, we can see that we use in the

tag There are two <script> tags. The first tag is used to reference the external JavaScript file test.js, and the second tag is the embedded JavaScript code to implement the myFunction function. By specifying different type attributes, we can use both inline and external JavaScript files in the same HTML file. <p>Conclusion<p>The above are several ways to introduce how to reference JavaScript scripts on web pages. Each method has its unique advantages and applicable scenarios. Choosing the method that best suits you can make us more Develop web pages well and provide a better user experience. </script>

The above is the detailed content of How to quote javascript in web pages. 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