JavaScript usage

<script></script> tag

To use JavaScript in HTML, you need to use the <script></script> tag and define The type attribute value is text/javascript, as shown in the previous alert pop-up prompt box example:

<script type="text/javascript">
alert("I am the prompt text ! ");
</script>

Usually JavaScript code alone is meaningless. JavaScript code is usually used in conjunction with HTML code, because JavaScript is inherently It was created to make up for the shortcomings of HTML.

JavaScript code can be directly embedded anywhere on the web page, but usually we put the JavaScript code in the <head>:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
    <script type="text/javascript">
    alert("我是提示文字!");
    </script>
</head>
<body>
    <div>图片及文字内容</div>
</body>
</html>


You can also place a JavaScript function into the <body> part of the HTML page:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>
    <script type="text/javascript">
    alert("我是提示文字!");
    </script>
    <div>图片及文字内容</div>
</body>
</html>

You can also put the JavaScript code into a separate .js file, and then introduce this file in HTML through <script src="..."></script>

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
 <script src="/static/js/abc.js"></script>
</head>
<body>
  <div>图片及文字内容</div>
</body>
</html>


JavaScript Functions and Events

Usually, we need to execute code when an event occurs, such as when the user clicks a button.

If we put JavaScript code into a function, we can call the function when the event occurs.

You will learn more about JavaScript functions and events in later chapters.



Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p>获取当前时间</p> <script> document.write(Date()); </script> </body> </html>
submitReset Code