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?

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.

  • Both you and Phil Haack rock! – Abe Miessler Apr 24 '11 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.

  • Does this work in 2010? I read that it did not as that was my first thought... – spevilgenius Apr 25 '11 at 20:35
3

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

  • Possibly, can you give me an example of what you are talking about? – Abe Miessler Apr 24 '11 at 19:15
  • If you have jQuery you would use this in lieu of window.onload $(documnet).ready(function() { alert("hello!"); }); – spevilgenius Apr 25 '11 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

  • That didn't work. If you read Phil Haacks article that David links to he mentions that you can't do that. – Abe Miessler Apr 24 '11 at 19:48
  • JavaScript doesn't natively support multiple handlers. – Rex M Apr 24 '11 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 Apr 25 '11 at 1:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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