Home >Web Front-end >JS Tutorial >How to reference javascript into html

How to reference javascript into html

藏色散人
藏色散人Original
2021-06-15 10:48:096985browse

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.

How to reference javascript into html

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(&#39;内联引入方式&#39;)">
</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!

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