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 448565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:40:57+00:00 2026-05-12T21:40:57+00:00

I am loading a JS file needed only for one part of my web

  • 0

I am loading a JS file needed only for one part of my web page, only when the user navigates to that tab on the page.

var scriptFile = document.createElement("script");
scriptFile.setAttribute("type", "text/javascript");
scriptFile.setAttribute("src", scriptURL);

I am trying to come up with an error handling mechanism for this dynamic loading, so that I can show an error message if the JS load times out or if the JS is unavailable in the location specified by the URL.

This is how I am trying to implement the error handling (for IE):

scriptFile.onreadystatechange = function() { // For IE
    if (this.readyState == 'complete' || this.readyState == 'loaded') {
        // Display content in tab
    } else {
        // Display error message
    }
}

This is causing one problem. When all is fine, and the JS is present as expected, I am getting the error message first, and then the correct content.

After some brainstorming, I found out that the readyState changes from “uninitialized” to “loading” first, and then to “loaded”. Its when the state is at “loading” that the error message is being displayed. After that, the state is changing to “loaded” at which point the content is being displayed.

Any ideas how to handle this so the error is not displayed initially?

  • 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-12T21:40:57+00:00Added an answer on May 12, 2026 at 9:40 pm

    Since there is no “oops I died!” state, you can’t display error messages.

    edit: removed the code since my point is that you can’t do that. With an AJAX request you can check the response and see if it’s a 404 but you can’t do that with a script tag.

    edit2: on second thought, why don’t you fetch the script via AJAX and inject a ‘<script type=”text/javascript”>’ + responseText + ‘</script>’ inside document.body?

    To check if an AJAX request has bombed, check the XMLHTTPRequest object’s status property. If it’s 404, well, it’s a 404.

    Also, you shouldn’t do this stuff by hand. I always suggest using Prototype or jQuery. Since I have been spoiled by Prototype, I shouldn’t try to explain how to check for the request’s status. Why don’t you ask how to handle AJAX requests in another question here? The pros will certainly tell you how to handle all kinds of failures, not just 404.

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

Sidebar

Related Questions

In one of my HTML page dynamically loading some file field while clicking a
Goal: Load Ext Framework (JS File) only when needed. Problem: When loading Ext.JS at
When loading a file, lets call it foobar.txt , like so: :b foo<tab> Where
I am working on code re-factoring of configuration file loading part in PHP. Earlier
I have web.rb file that works fine. I wanted to move all require files
I have a small demo executable wrote in C++ that depends only on one
I am loading a file data.imputation which is 2 dimensional in variable 'x' .
I am loading a file into a byte[]. By my understanding the byte[] should
internet-explorer aborts the EOT file loading progress sometimes, so the font does not load
What is the best way to handle browser-specific CSS file loading? Assume you are

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.