This article is a mirror article of machine translation, please click here to jump to the original article.

View: 10138|Reply: 0

[JavaScript] js-window.onload can only be called once!

[Copy link]
Posted on 2/26/2019 11:53:55 AM | | |
_addLoadEvent this method is to determine whether onload has been called multiple times, and if it is called multiple times, the multiple calls will be executed in Window.onload in turn

In fact, it's very simple, _addLoadEvent (func), pass a function in, and then determine whether window.onload has "bound" a function (i.e. typeof(window.onload) != 'function'), and if not, directly execute the passed func function. If it has been "bound", then define both the old function and the new function inside window.onload and execute it at once. i.e.: window.onload = function(){ oldonload(); func(); }

If a page has multiple window.onload definitions, only the last definition is often executed.

Let's talk about the difference between window.onload, $(document).ready(function(){}), and $(window).load(function(){})!

First, compare window.onload with $(document).ready(function(){}).



1. Execution time
    window.onload must wait until all elements of the page, including images, are loaded.
    $(document).ready()YesThe DOM structure is executed after it is drawn, without waiting for it to load

2. Write the number

     window.onload cannot be written at the same time, if there are multiple window.onload methods, it will only be executedOne
     $(document).ready() can be written at the same timemultiple, and can be executed

3. Simplify the writing method

     window.onload is not simplified
     $(document).ready(function(){}) can be abbreviated as $(function(){});

The difference between $(window).load(function(){}) and window.onload is that $(window).load(function(){}) can write multiple times





Previous:TeamViewer (permanently crack and unzip directly)
Next:java.io.FileNotFoundException: HADOOP_HOME and hadoop.home.dir are unset.
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com