0

I've had a quick look but cannot find a specific answer for this query so thought I'd ask the experts. I'm still learning HTML and Javascript.

I have some code where I'm using the standard "body onLoad" function to run an "initialise" function located in the HTML doco head section. I have another function called "populateList(){...}" in the head which basically populates an array with data from an external file.

I want to initialise this list by calling the "populateList" function from within the "initialize" function. I've tried using populateList(); and other permutations but this doesn't seem to work. I assume this is a syntax query and should be straightforward so haven't included any code but will if it makes this clearer. Does the "populateList" function need to be specified before the calling function or does it iterate through functions before loading.

Thanks

5
  • 1
    Without showing us your code, we cannot help you with fixing the errors in it. Commented Oct 17, 2014 at 6:00
  • what you have done ? Can you show it in a fiddle. Commented Oct 17, 2014 at 6:00
  • As others have said, we need to see your code AND you need to look in the browser error/debug console to see what is says there and report that here too. Commented Oct 17, 2014 at 6:03
  • 1
    Function declarations are processed before any code is executed, however that is per script element. Function expressions are evaluated in order during execution. Impossible to say if any of that is relevant without seeing your code. Commented Oct 17, 2014 at 6:04
  • Thanks folks for all who replied, (and RSquared for his answer) and apologies for the vagueness of the original query. I thought it would be fairly basic and actually sorted it myself after posting but thought I would see what other variations were possible. Of course it's "populateList();" which I used which does the trick. Commented Oct 24, 2014 at 2:08

1 Answer 1

1

Like this?

<html>
  <head>

    <script>
      function initialise()
      {
        alert('initialise');
        populateList();
      }
      function populateList()
      {
        alert('populate list');
      }
    </script>
  </head>
  <body onload="initialise()">

  </body>
</html>

Demo: http://plnkr.co/edit/5HgzbiIHDXUijdSGSTzO?p=preview

If thats the case, Javascript works by loading scripts from top to bottom - so you will need to declare them in order.

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

3 Comments

In that code, initialise and populateList can be declared in any order and in separate script elements anywhere in the document (in any order) since nothing will be called until the entire document has loaded and all script elements parsed.
That's true, sorry it's late and I don't know what I was thinking
That's OK. Since the OP hasn't defined what "… doesn't seem to work …" means, there is no criterion to evaluate "helpful" either. ;-) The issue could even be XHR, who knows?

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.