How to override Out Of the Box Javascript function in SharePoint

Sometime when we work with customization in sharepoint , may end up with overriding the javascript function which is part of core.js or form.js etc. For example if you try to make SharePoint 2010 site with HTML5 features then you may end up in overriding  many function in core.js or form.js to make / fix the issues related to IE9 compatability.

One of the approach you could take is create a the file for example Core.Fixes.js and put it into layouts\MyApplication\Scripts folder. Next step is add the functions which you want to overeride into the file.


Approach 1

Add the following line into the master page.
 <script type="text/javascript" src="/_layouts/MyApplication/MyApplicationScripts/core.ie9fix.js"></script>

Approach 2

For Example i have 4 function overrides to fix some IE9 issues so i register those functions like below to get the precedence over out of the box functions.

funtion applyIE9fix() {

_spBodyOnLoadFunctionNames.push("MergeAttributes");
_spBodyOnLoadFunctionNames.push("ProcessImnMarkers");
_spBodyOnLoadFunctionNames.push("RTE_DD_GetMenuFrame");

_spBodyOnLoadFunctionNames.push("RTE_GetEditorIFrame");
}

SharePoint pages are based on a master page that contains the “body” element. So content pages can't directly add a function to the body's onload event. In order to work around this limitation, SharePoint provides the “_spBodyOnLoadFunctionNames” array. When the body is loaded, the onload event handler executes each function whose name is contained in this array. We added “FunctionName” to the array so that it would run when the body's onload event fires.

Then i added following into my master page
    <script type="text/javascript" src="/_layouts/MyApplication/MyApplicationScripts/core.ie9fix.js"></script>
    <script type="text/javascript" >  
     applyIE9fix();
    </script>     

You could download IE9 fix javascript file from theis blog