So you want to start customizing a SharePoint page? Let’s say, you want to load or hide something in a page.
First steps include adding a content-editor-webpart on the page and adding some code on the source editor:
<script language="javascript"> _spBodyOnLoadFunctionNames.push("FunctionName"); function FunctionName(){ // Custom JavaScript methods } </script>
SharePoint provides the “_spBodyOnLoadFunctionNames” array. When the body is loaded, the onload event handler executes each function whose name is contained in this array. “FunctionName” was added to the array so that it would run when the body’s onload event fires.
In the function you can put all javascript you want.
At a client, this function was not called. Very weird problem, but we got a workaround really quickly from this blog
Adding an empty image on the page, and using the onload event of the image to start your code:
<img src="/_layouts/images/blank.gif" width="1" height="18" />
SharePoint already provides you with the blank image by default. Just add this line to you content-editor-webpart and add your function in the script tags.
Thank You. This helped me to call a javascript function on sharepoint page.
thank you very much
I tried your solution using image to call a javascript function I have and it works fine on Firefox but it doesn’t work for me in IE or Chrome. What my javascript function does is to get the breadcrumb generated by Sharepoint and retrieve only the current page name from it. Could you help please?
it doesn’t do the code ? Have you tried if it goes into the function ? (with something like a “hello world”)? you can always send me the code so I can take a look at it.