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

  • SEARCH
  • Home
  • 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 8765709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:21:25+00:00 2026-06-13T16:21:25+00:00

I’ve just learned an important fact about the execution of Javascript in case of

  • 0

I’ve just learned an important fact about the execution of Javascript in case of an error being thrown. Before I start making conclusions of this I’d better verify whether I am right.

Given an HTML page including 2 scripts:

<script src="script1.js" />
<script src="script2.js" />

script1:

doSomething();

script2:

doSomeOtherThing();

This effectively results in a single script being processed as one unit:

doSomething();
doSomeOtherThing();

In particular, if doSomething throws an error, the execution is broken. ‘script2’ is never being executed.

This is my "Lesson 1" – one might think since it is a separately included file it is not affected by script1. But it is. => see "late update" below


Now, if we change script2 as follows (presuming we have jQuery included somewhere above):

$(document).ready({ doSomeOtherThing(); });

and place the script before script2:

<script src="script2.js" />
<script src="script1.js" />

The order of execution is effectively still ‘doSomething()’ followed (sometime) by ‘doSomeOtherThing()’.

However it is executed in two "units":

  • doSomething is executed early as part of the document’s java script
  • doSomeOtherThing is executed when the document.ready event is processed.

If doSomeOtherThing throws an exception, it will not break the second processing "unit".

(I refrain from using the term thread because I reckon that all script is usually executed by the same thread, or more precisely this may depend on the browser.)

So, my Lession 2: Even though a JavaScript error may prevent any subsequent scripts from executing, it does not stop the event loop.


Conclusion 1

$(document).ready() does a great job in defining chunks of JavaScript code that should be executed independent on any other scripts in succeeding.

Or, in other words: If you have a piece of JavaScript and want to make sure it gets executed even if other scripts fail, place it within a $(document).ready().

This would be new to me in that I would only have used the event if the script depends on the document being fully loaded.


Conclusion 2

Taking it a step further it might be a good architecture decision to wrap all scripts within a $(document).ready() to make sure that all scripts are "queued" for execution. In the second example above, if script2.js was included after script1.js as in example 1:

<script src="script1.js" />
<script src="script2.js" />

An error in script1.js would prevent the doSomeOtherThing() from even being registered, because the $(document).ready() function would not be executed.

However, if script1.js used $(document).ready(), too, that would not happen:

$(document).ready(function() { doSomething(); });
$(document).ready(function() { doSomeOtherThing(); });

Both lines would be executed. Then later the event loop would execute doSomething which would break, but doSomeOtherThing would not be affected.

One more reason to do so would be that the thread rendering the page can return as soon as possible, and the event loop can be used to trigger the code execution.


Critique / Questions:

  • Was I mistaken?
  • What reasons are there that make it necessary to execute a piece of code immediately, i.e. not wrapping it into the event?
  • Would it impact performance significantly?
  • Is there another/better way to achieve the same rather than using the document ready event?
  • Can the execution order of scripts be defined if all scripts just register their code as an event handler? Are the event handlers executed in the order they were registered?

Looking forward to any helpful comments!

Late Update:

Like Briguy37 pointed out correctly, my observation must have been wrong in the first place. ("Was I mistaken – yes!"). Taking his simple example I can reproduce that in all major browsers and even in IE8, script2 is executed even if script1 throws an error.

Still @Marcello’s great answer helps get some insight in the concepts of execution stacks etc. It just seems that each of the two scripts is executed in a separate execution stack.

  • 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-06-13T16:21:27+00:00Added an answer on June 13, 2026 at 4:21 pm

    Your first assumption that they run as one script is incorrect. Script2 will still execute even if Script1 throws an error. For a simple test, implement the following file structure:

    -anyFolder
    --test.html
    --test.js
    --test2.js
    

    The contents of test.html:

    <html>
       <head>
           <script type="text/javascript" src="test.js"></script>
           <script type="text/javascript" src="test2.js"></script>
       </head>
    </html>
    

    The contents of test.js:

    console.log('test before');
    throw('foo');
    console.log('test after');
    

    The contents of test2.js:

    console.log('test 2');
    

    The output when you open test.html (in the console):

    test before test.js:1
    Uncaught foo test.js:2
    test 2 
    

    From this test, you can see that test2.js still runs even though test.js throws an error. However, test.js stops executing after it runs into the error.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question

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.