How to Add onLoad JavaScript event in Sharepoint?__java

Source: Internet
Author: User
Tags javascript array
How do i Add onLoad JavaScript event in SharePoint? SharePoint provides a JavaScript array "_spbodyonloadfunctionnames", any function is executed onLoad needs to is 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 the body load, reason; Content pages can ' t directly add a function to the body ' s OnLoad event if master page contains the <body> element (W Hich is mostly true).

This 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 and you'll notice the page onLoad event is handled by function _spbodyonloadwrapper, which Further calls Processdefaultonload, and this function executes the all function names added in array "_spbodyonloadfunctio Nnames ".

function Addloadevent (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 * *
}
);

Related Article

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.