Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Where do I put the javascript script

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "where to put the javascript script". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

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

This article operating environment: windows7 system, javascript1.8.5 version, Dell G3 computer.

The location of the JS script in Html

Where do I put the JavaScript script?

The JavaScripts in the HTML body section is executed when the page is loaded.

The JavaScripts in the HTML head section is executed only when it is called.

-

Where should JavaScript be put?

The JavaScripts in the page is executed immediately when the browser loads the page, and we don't want this to be always the case. Sometimes we want a script to be executed when the page is loaded, and sometimes we want to execute the script when the user triggers an event.

Scripts in the head section: scripts that need to be called to execute or scripts that trigger execution are placed in the head section of the HTML. When you put the script in the head section, you can ensure that the script is loaded before any call.

... .

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 often used to generate the content of the page.

... .

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

... .. .

-

Use of external scripts

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

Note: do not include tags in external script files!

When using an external script, point the value of the "src" property of the tag to the appropriate .js file:

This is the end of "where to put the javascript script". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report