html表单window.onload,html - JavaScript window.onload versus body.onload - Stack Overflow

Window (object) is sometimes reffered as global space container it contains all other objects (default and your custom one), it contains everything like indexedDB,session storage, cookies, your variables, your functions and everything within the browser page entirely.

Basically, as I wrote above window is an object, it stores everything either as a method or property. let me explain what is property and what is method,

(function my_obj(){

this.firstName="john";

var middleName = "XYZ"

this.lastName="doe";

this.fullName = function(){

return this.firstName+" "+this.middleName+" "+this.lastName

}

document.write(this.fullName())

})();

In this example I created Object using constructor function, in this example firstName, middleName and lastName are reffered as property, and fullName() is called methed, (You can say that method is another name for function within an object).

so consider window like this

window{

preset_obj:{};

indexedDB:{......};

.

.

.

.

.

.

.

.

.

.

.

//your code loads in the end

var my_var= "JS rocks!"

var anti_me= "Are You out of your Mind?? JS??Huh!"

var dont_care= "I Love COBOL"

}

but body my friend is different case, (not to mention document and body are two different objects). If you give body an ID and use document.getElementByID('ID'). it will return object containing all Visible elements that are displayed in the page. but it will not contain all other window stuff like cookies, session storage. Same is the Case with document. Document selects entire HTML code that you write nothing more nothing less. in other word it is object that contains everything that is enclosed within tags. hope this clears confusion

-----------------------UPDATE---------------------------------------------------

OK sorry didnt read your post correct.

window.onload will load when entire page is finished processing along with headers (not to be confused with head tag or HTML5 headertag).

document.onload will load just when frontend work (HTML, CSS and JS will finish loading) time difference isn't much unless you are sending 100KB+ code in headers, I'd say stick with window.onload unless your headers are large or you want to do something time specific to page display

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值