When not miniifying the scripts window.onload does work as expected.
But when I do minified my scripts using Uglify-js Folder it does not.
If I write the following and minify it :
window.onload = function() {
// My code
console.log( "HELLO TO YOU" );
};
It does not work in the browser.
But I f I write the following:
jQuery(document).ready(function() {
// My code
console.log( "HELLO TO YOU" );
});
It works fine!
Even just the console.log() alone does not print any thing using window.onload when minified.
Any ideas??
Here is the output:
window.onload = function() {
let e = document.querySelector("#main-header")
, t = e.offsetHeight
, o = document.querySelector("#et-main-area")
, n = document.querySelector("#wpadminbar");
o.style.marginTop = t + "px",
e.style.top = n ? n.offsetHeight + "px" : "0px"
}
,
jQuery(document).ready((function() {
let e = document.querySelector("#main-header")
, t = e.offsetHeight
, o = document.querySelector("#et-main-area")
, n = document.querySelector("#wpadminbar");
o.style.marginTop = t + "px",
e.style.top = n ? n.offsetHeight + "px" : "0px",
console.log("HELLO")
}
You have a comma operator between the function expression and the call to
jQuery().ready().This means the value assigned to the
onloadproperty will be the return value ofready()and not the function expression.Assuming the comma is generated by Uglify-js, then this is a bug in Uglify-js and you should probably report it there.
Asides:
addEventListeneris preferred overon*properties (and won't be vulnerable to this bug)DOMContentLoadedis probably better suited to your needs thanloaddeferattribute is probably better suited to your needs than any kind of event though.