Where does the javascript script appear?

藏色散人
Release: 2023-01-07 11:41:52
Original
3578 people have browsed it

Javascript scripts can appear in two locations, namely: 1. In the HTML body part, the js at this location will be executed when the page is loaded; 2. In the HTML head part, the js at this location js will be executed when called.

Where does the javascript script appear?

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

The location of JS script in Html

Where to put JavaScript script?

The JavaScripts in the HTML body part will be loaded when the page is loaded be executed.

JavaScripts in the HTML head section will be executed when called.

————————————————————————————

Where should JavaScript be placed

The JavaScripts in the page will be executed immediately when the browser loads the page. We don’t want this to always be the case. Sometimes we want a script to be executed when the page is loaded, and sometimes we want it to be executed when the user Execute the script when an event is triggered.

Scripts in the head section: Scripts that need to be called to be executed or scripts that are triggered by events are placed in the head section of HTML. When you place a script in the head section, you ensure that the script is loaded before any calls are made.

   
Copy after login

Scripts in the body section: Scripts that are executed when the page is loaded are placed in the body section of the HTML. Scripts placed in the body section are usually used to generate the content of the page.

  
Copy after login

The body and head parts can have scripts at the same time: You can put countless scripts in the file, so your file can have scripts in both the body and head parts.

   
Copy after login

——————————————————————————————–

External script Use

Sometimes you may want to run the same script on several pages without having to write the code repeatedly in each page. This is when you need to use external scripts. You can write the script in an external file and save it with a .js extension.

Note: Do not include the tag

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!