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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:45:25+00:00 2026-05-26T12:45:25+00:00

I have a tool, similar in ways to JSFiddle, that allows me to dynamically

  • 0

I have a tool, similar in ways to JSFiddle, that allows me to dynamically type in javascript and run it on a page. The code can be multiple lines, and typically will be.

Unfortunately if there is an exception in the code I type in, I can’t get the line number of the exception if I use eval() to run the code.

I found a partial solution, which is instead of using

try{
 eval(code);
 }
catch(e) {
 processException(e);
 }

to instead do something like this:

var s = document.createElement('script');
s.appendChild(document.createTextNode(
    "try{\n" + 
     code +
     "}catch(e){processException(e)}"));
document.body.appendChild(s);

Now, if the code throws an exception, and I look at the stack trace(in my processException() function) I can get a line number of the exception (in firefox and chrome, anyway).

That’s all well and good if it is actually a runtime exception, such as a variable not being defined. The problem is if there is a parse error / syntax error, such as mismatched parens or the like. I get nothing.

Is there any crazy workaround for this, that works on firefox and chrome, at a minimum? Eval within eval within script tag within Function object? I’m trying everything and haven’t found anything that works.

  • 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-26T12:45:26+00:00Added an answer on May 26, 2026 at 12:45 pm

    I found a reasonable solution finally.

    First, I set window.onerror to some function. This doesn’t get a full stack trace, but will get a file and line number.

    Then, I do this:

    var s = document.createElement('script');
    s.appendChild(document.createTextNode(
        "var someUniqueGlobalName = function () {\n" +
         code +
         "\n};";
    document.body.appendChild(s);
    

    Note that this doesn’t actually run my code, as it simply creates a function (in global scope, with the name ‘someUniqueGlobalName’ — which of course I’d really come up with a different name each time I do this).

    If there is a syntax error, it will be caught in the window.onerror function, and I can get the error type and line number (which of course I’ll have to subtract one from, since I added one line at the beginning).

    Now, I unset window.onerror.

    Finally, I run the code by calling someUniqueGlobalName() in a try/catch block. Here I can get a full stack trace with line numbers if there is a runtime error.

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

Sidebar

Related Questions

I'd like to have a tool window in Visual Studio that allows me to
I want to have a similar tool in Emacs as the following in Vim
I have a tool that I want to install on my main development box
I have a tool that is setup to query our Clearquest Database to return
Right now, I have a tool tip that pops up when I hover over
I have a GUI tool that manages state sequences. One component is a class
Is there is a tool similar to dir() for modules that will tell me
I've searched around and I can't find a simple commandline tool that will take
Anyone have any sample configurations using CruiseControl.net or similar tool?
So we have this tool, it's a web page, we drop a large piece

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.