JavaScript usage
JavaScript Usage
Scripts in HTML must be located between the tags.
Scripts can be placed in the
and sections of the HTML page.tell JavaScript where to start and end.
The lines between contain JavaScript:
You don’t need to understand the code above. Just understand that the browser interprets and executes the JavaScript code between
JavaScript 能够直接写入 HTML 输出流中:
您只能在 HTML 输出流中使用 document.write。 如果您在文档已加载后使用它(比如在函数中),会覆盖整个文档。
JavaScript functions and events
The JavaScript statements in the above example will be executed when the page is loaded. 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. JavaScript in or You can put an unlimited number of scripts in your HTML document. The script can be in the or section of the HTML, or both.The usual approach is to put the function in the
section, or at the bottom of the page. This allows them to be placed in the same location without interfering with the content of the page.JavaScript function in
In this example, we place a JavaScript function in the
of the HTML page ; part.This function will be called when the button is clicked:
我的 Web 页面
一个段落。
##JavaScript function in
In this example, we place a JavaScript function into the section of the HTML page. This function will be called when the button is clicked:My first web
This is a paragraph
External JavaScript
You can also save the script to in an external file. External files often contain code used by multiple web pages. The file extension for external JavaScript files is .js. If you need to use an external file, please set the .js file in the "src" attribute of the我的 Web 页面
一个段落。
注释:myFunction 保存在名为 "myScript.js" 的外部文件中。