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

This makes it possible to run js after the HTML page is loaded

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "to run js after loading the HTML page", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "so realize running js after loading the HTML page" article.

Js method:

_ window.onload=function () {varuserName= "xiaoming"

Alert (userName)

}

JQuery method, which needs to refer to the jQuery file:

$(document) .ready (function () {varuserName= "xiaoming"

Alert (userName)

})

Or its abbreviation:

$(function () {varuserName= "xiaoming")

Alert (userName)

})

It can be executed after DOM loading (earlier than _ window.onload). .ready () can appear multiple times on the same page.

The main difference between the two: _ Window.onload=function () {}:

The _ window.onload event is triggered when a document is fully downloaded to the browser. This means that all elements on the page are operable for js, which means that all elements on the page are not executed until they have been loaded. This situation is very beneficial for writing functional code because there is no need to consider the order in which it is loaded.

$(document) .ready (function () {})

Called when the DOM is fully ready and ready to use. Although this also means that all elements are accessible to the script, it does not mean that all associated files have been downloaded. In other words, when the HTML download completes and parses to the DOM tree, the code executes.

Using $(document) .ready (function () {}) is generally better than using onload event handlers, but it must be clear that attributes such as the height and width of the image may not be valid at this time because the support file may not have been loaded yet.

Note: there will be problems with the method of putting js at the bottom of the page and the method of using defer= "defer". It's best to use the $(document) .ready (function () {}) function.

The above is about the content of this article about "running js after loading the HTML page". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

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

Development

Wechat

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

12
Report