Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7620061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:50:49+00:00 2026-05-31T03:50:49+00:00

I have a website with three files: index.html <html> <head> <script src=First.js></script> <script src=Second.js></script>

  • 0

I have a website with three files:

index.html

<html>
<head>
<script src="First.js"></script>
<script src="Second.js"></script>
</head>
<body></body>
</html>

First.js

window.onload = Main;

Second.js

function Main() { var foo = 1; }

When I open the page, I expect the variable ‘foo’ to be set to ‘1’. Instead, when I open the page, it breaks indicating ‘Main is not defined’ and ‘Main()’ is never called.

If the ‘.onload’ event for the window isn’t supposed to fire until the page is fully loaded and thus assumingly both scripts have been loaded, why doesn’t the window object have a reference to the ‘Main()’ method in ‘Second.js’? Should ‘Main()’ be a globally accessible function?

NOTE: I realize I can change the order in which I load the scripts and then my code would work, but that’s not the purpose of my question. What I really want to do is validate my assumption.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:50:51+00:00Added an answer on May 31, 2026 at 3:50 am

    It doesn’t work because the code in <script> elements are run sequentially and independently (this assumes synchronous scripts, the default). That is, two different <script> sections cannot forward-reference each other. Now, JavaScript “hoists” function name () {} constructs within the same context, so this would have worked “out of order” in a single <script>.

    Preamble / warning:

    Consider not using onload. If something else tries to use it (onload), then something will likely break. (I recommend jQuery because it “gets this right” and “is so darn easy”… then again, so do many other frameworks. Pick one 🙂

    For instance, this would work in jQuery:

    jQuery(function () { Main() })
    

    Explanation of why it works (and how to fix it without reordering scripts or using jQuery):

    Note that in this case the jQuery internal event handler is called, which calls the anonymous callback, which then calls Main (which is now resolves to a function-object). The “similar” code here would be:

    window.onload = function () { Main() }
    

    These work because Main is not evaluated until the onload has occurred, in which case all the [synchronous] <script> elements have been executed. (Please see my comment as to what it means to evaluate Main.)

    On the other hand, window.onload = Main (or jQuery(Main)) evaluate Main right-then and use the resulting value; as others have noted, Main is not set (“is undefined”) at this point due to the ordering of the <script> elements (they run sequentially).

    Please refer to the note at top as to why to not to use onload directly 😉

    Happy coding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a website with nanoc. I have my main index file and now
I have a website that has an index.php, a few simple .html pages and
I have website A: http://dl.dropbox.com/u/3264697/calc/v2/index.html It is working as intended, 100%. I have website
I have a website with three domains .com, .de and .it Each domain needs
I have a website going that takes a user's uploaded image, and makes three
I have a MySql Master server replicating to three Slaves. A legacy website is
I have an ASP.NET 4 WebForms website running off IIS 7, we have three
I have a asp.net website, there is a searchResult.aspx, it runs a sql script
I have a website that is done by html. So the home file is
I have an xml file called index.xml this is in my website here: www.mywebsite.com/siteregistry/index.xml

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.