How to add onload JavaScript event in SharePoint?

Source: Internet
Author: User
How to add onload JavaScript event in SharePoint?
SharePoint provides a JavaScript array "_ spbodyonloadfunctionnames", any function to be executed onload needs to be added to this array e.g.

_ Spbodyonloadfunctionnames. Push ("executemyfunctiononload ");

Now, why does your JavaScript function doesn' t execute if you just register it
With clientscript. registerstartupscript?

Actually, content pages can't execute JavaScript function on body load, reason; content pages can't directly add a function to the body's onload event if master page contains the <body> element (which is mostly true ).

This array is basically a part of init. JS located in "c: \ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 12 \ template \ layouts \ 1033 \". if you open the Javascript file, you will notice the page onload event is handled by function _ spbodyonloadwrapper,
Which further callprocessdefaultonload, and this function executes all the function names added in array "_ spbodyonloadfunctionnames ".

FunctionAddloadevent(Func ){
VaR oldonload = Window. onload;
If (typeof window. onload! = 'Function '){
Window. onload = func;
} Else {
Window. onload = function (){
Oldonload ();
Func ();
}}}
Addloadevent(Nameofsomefunctiontorunonpageload );
Addloadevent(Function (){
/* More code to run on page load */
}
);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.