SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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() {
    window.onload = test;
<input id="tb_AC" runat="server" /> 
<br />

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

share|improve this question
up vote 11 down vote accepted

The reason is that you are hijaking the onlod event from the SharePoint javascript. See this post for help

share|improve this answer
Both you and Phil Haack rock! – Abe Miessler Apr 24 '11 at 19:29

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.

share|improve this answer
Good to know, thanks – Abe Miessler Apr 24 '11 at 19:48
Does this work in 2010? I read that it did not as that was my first thought... – spevilgenius Apr 25 '11 at 20:35

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

share|improve this answer
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

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

share|improve this answer
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
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 posting your answer, you agree to the privacy policy and terms of service.

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