Example of inserting JavaScript code in HTML

Source: Internet
Author: User
Tags script tag


This article mainly introduces the example of inserting JavaScript code in HTML, nested in HTML code is the basic function of JavaScript, need friends can refer to the following



JavaScript code can be included anywhere in an HTML document. But there are also the best ways to include JavaScript in HTML files.



In... Part.



In... Part.



In... And... Part.



Scripts and external files, and then include in the ... Part.



In the following sections, we'll see how different JavaScript approaches can be included:



In the JavaScript ... Part:



If you want to run a script on some events, when the user clicks somewhere, such as, then the script should be the head, as follows:




This will produce the following results:



JavaScript in ... Part:



If you need a script to load the page so that the script generates page content to run, the script is in the part of the document. In this case, you do not have to use all of the features defined by javascript:




JavaScript in and parts:



You can put the JavaScript code in and section exactly as follows:



In JavaScript external files:



When you start working and use JavaScript more extensively, you may find that there are cases where you reuse the same JavaScript code on multiple pages in your site.



You are not limited to the same code that will remain in multiple HTML files. The script tag provides a mechanism that allows you to store JavaScript in an external file and then include it in an HTML file.



Here is an example of how to use script tags and SRC attributes to include external JavaScript files in HTML code:




Using JavaScript from an external file source, you need to use extensions to write down all the JavaScript code in a simple text file ". js", and then include the file as shown in the figure above.



For example, you can keep the contents of the following file Filename.js, and then include the Filename.js file and use Sayhellofunction in the HTML file:




function SayHello () {alert ("Hello World")}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.