How to Add onLoad JavaScript event in SharePoint?
2013-01-18 19:20
585 查看
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 calls ProcessDefaultOnLoad, and this function executes all the function names added in array "_spBodyOnLoadFunctionNames".
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 */
}
);
相关文章推荐
- how to add Javascript and CSS in page layout with sharepoint 2013
- How to copy files between sites using JavaScript REST in Office365 / SharePoint 2013
- How to redirect to a Custom Page for Event Receiver in SharePoint 2010
- How To Write In Sharepoint Log File 怎么对自定义的MOSS代码写日志
- How to Enable Project Server License in SharePoint?
- How to change service accounts and service account passwords in SharePoint Server 2007 and in Windows SharePoint Services 3.0
- How to change service accounts and service account passwords in SharePoint Server 2007 and in Windows SharePoint Services 3.0
- How to get current date time in SharePoint Site Time Zone
- How to Setup Load Balance in SharePoint 2010 Farm
- how to change folder's sort number in sharepoint?
- How to delete a large number of data in SharePoint for List when refreshing data?
- 四 错误处理篇 How to Fix Login Failed For User NT AUTHORITY IUSRS in SharePoint 2013 ...
- How to unlock the locked tasks in Sharepoint Workflow
- 转:How to create your own custom 404 error page and handle redirect in SharePoint 2007 (MOSS)?
- How to remove the ribbon tabs in list forms in SharePoint 2010
- Prepare to deploy SharePoint add-in
- How to Create Multilingual Webpart in SharePoint 2010? Use Localization in Globalization.
- How to create custom navigation menu in SharePoint with XML data source 使用XML数据源在SharePoint创建自定义导航菜单
- How to Enable or Disable Document Property Promotion (ParserEnabled) in SharePoint 2013
- How to add share folder in svn(propset)