Home >Web Front-end >JS Tutorial >How to reference javascript into html
How to reference javascript into HTML: 1. Add js code in the event attribute of the element; 2. Add script anywhere in the html page; 3. Write js code in a separate js file.
The operating environment of this article: windows7 system, html5&&javascript version 1.8.5, Dell G3 computer.
How to reference javascript into html?
Three introduction methods
1. Inline: Add js code in the event attribute of the element, and js will be executed when the event is triggered
2 . Internal: Add script anywhere in the html page (recommended in the head)
3. External: Write js code in a separate js file and introduce it through the src attribute of script. If the script tag introduces the file, Cannot continue to write js code in the tag body
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <!-- 内部引入方式--> <script type="text/javascript"> alert("使用内部引入"); var x = "123"; if(isNaN(x)){ alert("不是数"); }else{ alert("是数"); } </script> <!-- 外部引入方式--> <script type="text/javascript" src="first.js"></script> </head> <body> <!-- 内联方式--> <input type="button" value="内联引入方式" onclick="alert('内联引入方式')"> </body> </html>
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to reference javascript into html. For more information, please follow other related articles on the PHP Chinese website!