Today I needed to fire some JavaScript code when a SharePoint page loaded. There are several ways to do this as far as I can tell, but there is only one way which I liked. After your JavaScript code you should include the following code:
_spBodyOnLoadFunctionNames.push("your method");
My customer needed a (pop-up) page that was printed and closed immediately after it was opened. I did this in the following way:
StringBuilder script = new StringBuilder();
script.Append("function onLoad()");
script.Append("{");
script.Append(" window.print();");
script.Append(" window.close();");
script.Append("}");
script.Append("_spBodyOnLoadFunctionNames.push(\"onLoad\")");
script.Append("function onLoad()");
script.Append("{");
script.Append(" window.print();");
script.Append(" window.close();");
script.Append("}");
script.Append("_spBodyOnLoadFunctionNames.push(\"onLoad\")");
You should of course not forget to register your JavaScript. :-)