A brief discussion on the placement of script in HTML

A brief discussion on the placement of script in HTML

I used to think that script could be placed anywhere in HTML, but today when I was working on a requirement, I corrected my wrong idea - the location of the script cannot be placed randomly.

First, I want to implement a select tag with two options, yes and no. However, when initializing, the select tag is required to select an empty value by default, so I added a method to delete the empty value when clicking:

XML/HTML CodeCopy content to clipboard
  1. <!DOCTYPE html >   
  2. < html >   
  3. < head >   
  4. < script   src = "jquery/jquery-1.11.1.min.js" > </ script >   
  5. </ head >   
  6. < script >   
  7. $('#checkcash').click(function () {
  8. if ($('#checkcash').val() == '0') {
  9. $("#checkcash option[ value = '0' ]").remove();
  10. }
  11. });
  12. $("#alert").click(function(){
  13. alert("1123");
  14. })
  15. </ script >   
  16. < body >   
  17. Has the cash been withdrawn ?   id = "checkcash"     style = "width: 181px" >   
  18.                              < option   selected = "selected"   value = "0" > </ option >   
  19.                              < option   value = "1" > Yes </ option >   
  20.                              < option   value = "2" > No </ option >   
  21.                              </ select >   
  22.                                 
  23.                              < input   type = 'button'   id = 'alert'   value = "anwo" >   
  24. </ body >   
  25.   
  26.   
  27. </ html >   

However, this did not achieve the desired effect. At first, I thought it was a jQuery syntax error, so I kept checking and modifying it online, but it didn't work. Later, I suddenly thought that I should put the script at the end. I tried it, and it turned out to be ok. Then I realized that this was not the case.

Later I checked the reason and found that it was because the HTML file was executed from top to bottom, but the order of the introduced CSS and JavaScript was different. When the CSS was introduced and loaded, the program still executed downwards, and when it executed to the <script> script, the thread was interrupted, and the program continued to execute after the script script was executed. Therefore, script is usually placed after body to avoid delay and blocking caused by long execution of script. To achieve some page effects, it is necessary to dynamically load some js scripts in advance, so these scripts should be placed before <body>. Secondly, you cannot put the js that needs to access DOM elements before the body, because the DOM has not yet started to be generated, so the js that accesses the DOM elements before the body will fail or be invalid. Because of this, I added methods to it when the DOM was not generated, which led to this.

I really should have learned more about many things, but I didn't go into them in depth. Keep working on it!

ps: In fact, there is another way, which is to use jquery's initialization page method. You can also add the click event added to the label above to $(function(){}). The principle is the same as above. This method will not be executed until the page is loaded, so it doesn't matter where you put it!

The above brief discussion on the placement of script in HTML is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

<<:  Solution to the garbled problem of web pages when the encoding is set to utf-8

>>:  Specific use of MySQL operators (and, or, in, not)

Recommend

Using js to achieve the effect of carousel

Today, let's talk about how to use js to achi...

Use Navicate to connect to MySQL on Alibaba Cloud Server

1. First enter the server's mysql to modify p...

Building an image server with FastDFS under Linux

Table of contents Server Planning 1. Install syst...

Vue+Openlayer uses modify to modify the complete code of the element

Vue+Openlayer uses modify to modify elements. The...

Vue realizes web online chat function

This article example shares the specific code of ...

Solve the error "Can't locate ExtUtils/MakeMaker.pm in @INC"

When installing mha4mysql, the steps are roughly:...

BUG of odd width and height in IE6

As shown in the figure: But when viewed under IE6...

Tutorial on installing MySQL under Linux

Table of contents 1. Delete the old version 2. Ch...

Mybatis paging plug-in pageHelper detailed explanation and simple example

Mybatis paging plug-in pageHelper detailed explan...

Completely uninstall MySQL database in Windows system to reinstall MySQL

1. In the control panel, uninstall all components...

Detailed explanation of JavaScript implementation of hash table

Table of contents 1. Hash table principle 2. The ...

Steps to set up HTTPS website based on Nginx

Table of contents Preface: Encryption algorithm: ...

SQL implementation LeetCode (185. Top three highest salaries in the department)

[LeetCode] 185. Department Top Three Salaries The...