There are two ways to import JavaScript into html: 1. Use the "<script>js code</script>" statement to embed JavaScript code in the HTML page; 2. In HTML, use the src of the script tag Properties are imported into external JavaScript script files.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
How to import JavaScript into HTML documents
1. Embed JavaScript code in HTML documents
Embedding JavaScript scripts in HTML pages requires the use of the <script> tag. Users can directly write JavaScript code in the <script> tag. The specific steps are as follows. </script>
Step 1, create a new HTML document and save it as test.html.
Step 2, insert a <script> tag inside the <head> tag. </script>
Step 3, set the type="text/javascript" attribute for the <script> tag. </script>
The default script type of the <script> tag in modern browsers is JavaScript, so the type attribute can be omitted; if compatibility with earlier versions of browsers is considered, the type attribute needs to be set. </script>
Step 4. Enter the 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 id="PHP中文网-https-www-php-cn">PHP中文网:https://www.php.cn/</h1>"); </script> </head> <body></body> </html>
Step 5. Save the web page document and preview it in the browser. The display effect is as shown in the figure. In JavaScript scripts, document represents a web page document object; document.write() represents calling the write() method of the Document object to write an HTML string in the current web page source code."<h1 id="PHP-Chinese-website-https-www-php-cn">PHP Chinese website: https ://www.php.cn/</h1>
".
2. Introducing external JavaScript code script files into HTML documents
JavaScript programs can not only be placed directly in HTML documents, It can also be placed in a JavaScript script file. JavaScript script files are text files with a .js extension that can be edited using any text editor.
Commonly used text editors include Notepad in Windows systems, Vim, Sublime Text, Notepad, etc. in Linux systems. For beginners, it is recommended to use a text editor to write JavaScript code first, which will help us remember JavaScript syntax, keywords, functions, etc. When it comes to the actual development stage, you can choose some more professional code editors, such as Visual Studio Code (referred to as "VS Code"), WebStorm (paid), Atom, etc., which can improve development efficiency.
The steps to create a new JavaScript file are as follows.
Step 1, create a new text file and save it as test.js. Note that the extension is .js, which indicates that the text file is a JavaScript type file.
Step 2, open the test.js file and write the following JavaScript code in it.
alert("PHP中文网:https://www.php.cn/");
Step 3, save the JavaScript file. It is recommended to put JavaScript files and web page files in the same directory. In the above code, alert() represents the method of the Window object. Calling this method will pop up a prompt dialog box displaying the parameter string "Hi, JavaScript!".
JavaScript files cannot run independently and need to be imported into the web page and executed through the browser. JavaScript files can be imported using the <script> tag. </script>
Step 4, create a new HTML document and save it as test.html.
Step 5, insert a <script> tag inside the <head> tag. Define the src attribute and set the attribute value to a URL string pointing to an external JavaScript file. The code is as follows: </script>
<script type="text/javascript" src="test.js"></script>
Step 6. Save the web page document and preview it in the browser. The display effect is as shown in the figure. 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>
The <script> tag that defines the src attribute should no longer contain JavaScript code. If the code is embedded, only the external JavaScript file will be downloaded and executed, and the embedded code will be ignored. </script>
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to import JavaScript into html. For more information, please follow other related articles on the PHP Chinese website!

React'slimitationsinclude:1)asteeplearningcurveduetoitsvastecosystem,2)SEOchallengeswithclient-siderendering,3)potentialperformanceissuesinlargeapplications,4)complexstatemanagementasappsgrow,and5)theneedtokeepupwithitsrapidevolution.Thesefactorsshou

Reactischallengingforbeginnersduetoitssteeplearningcurveandparadigmshifttocomponent-basedarchitecture.1)Startwithofficialdocumentationforasolidfoundation.2)UnderstandJSXandhowtoembedJavaScriptwithinit.3)Learntousefunctionalcomponentswithhooksforstate

ThecorechallengeingeneratingstableanduniquekeysfordynamiclistsinReactisensuringconsistentidentifiersacrossre-rendersforefficientDOMupdates.1)Usenaturalkeyswhenpossible,astheyarereliableifuniqueandstable.2)Generatesynthetickeysbasedonmultipleattribute

JavaScriptfatigueinReactismanageablewithstrategieslikejust-in-timelearningandcuratedinformationsources.1)Learnwhatyouneedwhenyouneedit,focusingonprojectrelevance.2)FollowkeyblogsliketheofficialReactblogandengagewithcommunitieslikeReactifluxonDiscordt

TotestReactcomponentsusingtheuseStatehook,useJestandReactTestingLibrarytosimulateinteractionsandverifystatechangesintheUI.1)Renderthecomponentandcheckinitialstate.2)Simulateuserinteractionslikeclicksorformsubmissions.3)Verifytheupdatedstatereflectsin

KeysinReactarecrucialforoptimizingperformancebyaidinginefficientlistupdates.1)Usekeystoidentifyandtracklistelements.2)Avoidusingarrayindicesaskeystopreventperformanceissues.3)Choosestableidentifierslikeitem.idtomaintaincomponentstateandimproveperform

Reactkeysareuniqueidentifiersusedwhenrenderingliststoimprovereconciliationefficiency.1)TheyhelpReacttrackchangesinlistitems,2)usingstableanduniqueidentifierslikeitemIDsisrecommended,3)avoidusingarrayindicesaskeystopreventissueswithreordering,and4)ens

UniquekeysarecrucialinReactforoptimizingrenderingandmaintainingcomponentstateintegrity.1)Useanaturaluniqueidentifierfromyourdataifavailable.2)Ifnonaturalidentifierexists,generateauniquekeyusingalibrarylikeuuid.3)Avoidusingarrayindicesaskeys,especiall


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
