当前位置:网站首页>The difference between script in head and body
The difference between script in head and body
2022-06-23 02:22:00 【User 1349575】
The loading order is different ,html It is loaded from top to bottom . If the network speed is slow js Code placed in body Users at the bottom will see the structure of the web page first , etc. js The special effect will not appear until the loading is completed .
difference : stay HTML body In section JavaScript It will be executed when the page is loaded . stay HTML head In section JavaScripts Will be executed when called , But preload the home page and the rest of the code .
1. JavaScript Where should I put it
head Script in section :
Scripts that need to be called to execute or scripts triggered by events to execute are placed in HTML Of head In the part . When you put the script in head In part , This ensures that the script is loaded before any call , This makes the code more powerful ; For example, yes. *.js Early invocation of files . That is to say, put the code in <head> When the page is loaded , The code is loaded at the same time , you are here <body> There is no need to load code when calling the , The speed increases , This difference is invisible in small programs , When running large and complex programs , You can see .
body Script in section :
The script that executes immediately when the page is loaded is placed in HTML Of body part . Put it in body Part of the script is usually used to generate the content of the page .
body and head Some can have scripts at the same time :
You can put countless scripts in the file , So your file can be found in body and head Some scripts exist at the same time .
2. If you put javascript Put it in head Words in Li , Is parsed first , But this time body Not resolved yet .
( routine html The structure is head before ,body After ) If head Of js The code needs to pass in a parameter ( stay body When calling this method, middle note , Parameters will be passed in ), And you need to call this parameter for a series of operations , Then you will definitely report an error at this time , Because the parameter of the function is undefined (undefined).
3. from JavaScript Consider the direction of page download performance :
Because the script will block the download of other resources ( Such as pictures, etc. ) And page rendering , Until the script is completely downloaded and executed , The rendering of the page will continue , Therefore, it is recommended that all <script> The label should be placed as far as possible <body> The bottom of the label , To minimize the impact on the whole page download .
边栏推荐
- Single chip microcomputer (STC series 8051 core single chip microcomputer)
- Initial structure
- Spread spectrum and frequency hopping
- What is ISBN code and how to make it
- [target tracking] open source | polytrack: use boundary polygons to quickly track and segment multiple targets, instead of bounding box and mask tracking
- The commercial s2b2b e-commerce platform of aquatic industry improves the competitiveness of enterprises and creates a strong engine for industrial development
- Why is BeanUtils not recommended?
- Targeted and ready to go
- [CodeWars] Convert Decimal Degrees to Degrees, Minutes, Seconds
- Operator part
猜你喜欢

Dynamic address book in C language (add, delete, modify, check (duplicate), delete, sort and export)

2022-1-14

Digital circuit logic design

Information theory and coding

Circuit analysis (circuit principle)

Anaconda creates a new environment encounter pit

Quick sorting C language code + auxiliary diagram + Notes

2021-11-11

For Xiaobai who just learned to crawl, you can understand it after reading it

Application and challenge of ten billion level map data in Kwai security intelligence
随机推荐
Garbled code of SecureCRT, double lines, double characters, unable to input (personal detection)
Three methods for solving Fibonacci sequence feibonacci (seeking rabbit) - program design
Ch340 and PL2303 installation (with link)
1. Mx6u bare metal program (2) - Lighting master (imitating 32 register version)
Branch and loop statements (including goto statements) -part1
How to set up an H5 demo of easyplayer locally to play h265 video streams?
Pywebio to quickly build web applications
1.3-1.4 web page data capture
Source code analysis | activity setcontentview I don't flash
9. class and object practice and initialization list
5g access network and base station evolution
Stop automatically after MySQL starts (unable to start)
Docker installs mysql5.7 and mounts the configuration file
Rebirth -- C language and the story I have to tell (text)
Hypervisor Necromancy; Recover kernel protector (1)
You can be what you want to be
Performance test -- 14 detailed explanation of performance test report and precautions
Branch and loop statements (including goto statements) -part2
How to locate memory leaks
SetTimeout and setinterval execution time