0

I have just started to use jQuery from prototype and am working with the load function to execute some scripts once the page has been loaded:

$(window).load(
function()
{
Stuff happens
}
);

However, how do you add a list of functions and control the order in which they execute? It's difficult to put everything I want to load in the 'stuff happens' area, so i need to be able to add and remove stuff from an array and change the order. Then one page load, execute that list.

Any clues?

2
  • The "change the order" part is tricky, can you provide an example of this? Commented Jun 28, 2010 at 15:33
  • This doesn't sound so much like a specific issue as it is a general difficulty with your control flow, so your question isn't specific enough to provide an answer. You'll have to do more to explain what you are trying to do, and the exact issue your having trouble with if you want an answer. Commented Jun 28, 2010 at 16:17

1 Answer 1

1

You might try something like this.

First, set up your functions:

var func1 = function(){ alert('this'); };
var func2 = function(){ alert('that'); };

Next, make an array to contain them in the desired order:

var funcs = [
  function(){alert('a');}, // anonymous, defined inline
  function(){alert('b');}, 
  func2,                   // or by reference
  func1,                   // you control the sequence
];
funcs.push( function(){alert('c');} ); // etc

Finally, in your load handler:

$(window).load( function(){
  $.each( funcs, function(i,thisfunc){ // or native js for() if you prefer
    thisfunc();
  });​
});

When the load event triggers, you'll get the alerts a b that this c. This isn't all that different from how jQuery handles this sort of thing with its private readylist array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.