1

I have many places in a website where I want javascript code to be executed when the page has loaded, as well as when the area around code is rerendered, e.g.

<div id="blah">
<script>
addLoadEvent(function (){
  submenus.init('somedata');
});

</script>
loads of other stuff here
</div>

mostly the code is initialization code for menus, sliders etc etc that exist within the div.

I wanted to handle these code snippets by adding the function calls to the window.onload event.

That works fine for loading the page, BUT if I rerender the area, the window.onload event is not triggered... I have tried this in different browsers, without success.

I cannot use the oncomplete from the elements that cause the rerender, since they are all over the place, and they should not really know about the implementation of the components.

Any ideas how to solve this?

JohnSmith
  • 4,538
  • 9
  • 25
  • 25
  • If your page is updated using ajax calls, window.onload won't fire, see http://stackoverflow.com/questions/1694338/window-onload-equivalent-for-ajax-applications – Adam Apr 01 '11 at 20:40
  • hm, any idea how to trigger an event by rerender (without using oncomplete)? i cant seem to figure out a way to find out if the code was rerendered or normally loaded.... – JohnSmith Apr 03 '11 at 11:30

2 Answers2

1

JQuery has a great solution, but I'm not sure you would want to load JQuery just for this.

http://api.jquery.com/ready/

Caimen
  • 2,623
  • 2
  • 26
  • 43
  • thanks for the comment, would be easiest, but unfortunately we cannot use jquery in this project (not my decision to make..) – JohnSmith Apr 03 '11 at 11:28
0

doh! the problem i had was that i was loading the javascript asynchron (forgot about it; it was built in years ago), and that caused javascript errors in FF4, when it tried calling functions that didnt exist, hence i started trying to get it to work with window.onload.

i just load the external javascripts normally, and everything works fine

JohnSmith
  • 4,538
  • 9
  • 25
  • 25