11

This is related to an earlier question that I posted:

Can't edit page after adding custom web part!

Basically, I have created a web part with some javascript. When I add this web part to a page, I can no longer click on the edit icon to make changes to the page. If I retract the web part I can edit again.

I have created a slimmed down version that reproduces the problem below:

<script type="text/javascript">
    function test() {
        alert('test');
    }
    window.onload = test;
</script>
<input id="tb_AC" runat="server" /> 
<br />

If I comment out the window.onload = test; then edit starts working again. Thoughts?

4 Answers 4

11

The reason is that you are hijacking the onload event from the SharePoint javascript.

See this post for help:

Stop The Window.Onload Madness.

1
  • Both you and Phil Haack rock! Commented Apr 24, 2011 at 19:29
13

SharePoint also provides the _spBodyOnLoadFunctionNames function, which allows you to specify what script to run on page load. If you specify your function there, it is less likely to interfere with SharePoint's own script.

1
  • Does this work in 2010? I read that it did not as that was my first thought... Commented Apr 25, 2011 at 20:35
3

Could you not just use jQuery and the document.ready() function?

2
  • Possibly, can you give me an example of what you are talking about? Commented Apr 24, 2011 at 19:15
  • If you have jQuery you would use this in lieu of window.onload $(documnet).ready(function() { alert("hello!"); }); Commented Apr 25, 2011 at 20:38
0

Try window.onload += test; Instead, so you dont overwrite what other javascripts (such as the ribbon) have hooked up on the event

3
  • That didn't work. If you read Phil Haacks article that David links to he mentions that you can't do that. Commented Apr 24, 2011 at 19:48
  • JavaScript doesn't natively support multiple handlers.
    – Rex M
    Commented Apr 24, 2011 at 23:36
  • 2
    JavaScript actually DOES natively support multiple handlers, though it doesn't have the += syntax. You have to call the DOM methods attachEvent/addEventListener (depending on which browser you're in).
    – Zach
    Commented Apr 25, 2011 at 1:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.