Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

4 Answers

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

http://haacked.com/archive/2006/04/06/StopTheWindow.OnloadMadness.aspx

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

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
add comment

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
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
add comment

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
add comment

Your Answer

 
discard

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.