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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:12:21+00:00 2026-05-25T20:12:21+00:00

javascript which is introduced from a string instead of a .js file is difficult

  • 0

javascript which is introduced from a string instead of a .js file is difficult to debug because errors reference the url of the script file containing the error. If many strings are introduced, it is difficult to tell which string(script) is the source of the error.

I have a large number of objects. Each object has a string which will dynamically define its javascript functions. Once the functions have been defined, I need a way for errors that occur during the execution of these dynamically defined functions to report some kind of information that indicates which string/object is the source of the error.

EXAMPLE 1: Inject functions using eval()

If you use eval() to insert the string as javascript, the error url is undefined.

str = "test_fn = function(){undefined_fn();}";
eval(str);
test_fn();

will yield the following error:

message: "ReferenceError: Can't find variable: undefined_fn"
url: undefined
line: 1

So in this case, the error url is undefined.

EXAMPLE 2: Inject functions using a script tag

If instead of eval, you use a script tag to insert the string as javascript, the error url references the html file.

str = "test_fn = function(){undefined_fn();}";
if(!document.head) document.head = document.getElementsByTagName('head')[0];
var elem = document.createElement('script');
elem.text = str;
document.head.appendChild(elem);
test_fn();

will yield the following error:

message: "ReferenceError: Can't find variable: undefined_fn"
url: "testpage.html"
line: 1

So in this case, the error url is the html page.

I need a way for errors that occur during the execution of these dynamically defined functions to report to the developer some kind of information that indicates which string is the source of the error. I know that I could use the server to dynamically generate a large number of source files and then set script.src for each file, however I would like to avoid referencing hundreds of script files. I would rather prefer to understand how to perhaps modify the url that is reported, or pass some additional information that could be reported to the developer.

I would prefer a cross browser solution, but since this is for development only, it is not necessary.

Errors in these examples were reported using: window.onerror = function(message, url, line) {}

  • 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-25T20:12:21+00:00Added an answer on May 25, 2026 at 8:12 pm

    What about this:

    function SuperError(msg, location) {
       this.message = msg;
       this.url = location;
    }
    
    str = "test_fn = function(){try { undefined_fn(); } catch(e) {throw new SuperError('undefined_fn is undefined', 'in dynamic script head')}}";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a regex (JavaScript) which will extract shortforms from a string for example
I have a javascript which receives info from a servlet using jQuery: $.get(authenticate, {badge:$('input#badge').val()},
This Javascript which uses functions from jQuery is quite handy but getting feedback on
I have jquery script, which should send response to my file. If everything good,
I have a javascript which I didn't write but I need to use it
I have a large javascript which I didn't write but I need to use
I have some javascript which catches changes to a form then calls the form's
In my quest to learn javascript (which seems to be my latest source of
I'm writing BNF for JavaScript which will be used to generate a lexer and
I have a timer in my JavaScript which needs to emulate clicking a link

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.