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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:45:30+00:00 2026-06-06T08:45:30+00:00

It should be simple to create JavaScript intepreter in JavaScript using eval . I

  • 0

It should be simple to create JavaScript intepreter in JavaScript using eval. I got this (using jQuery terminal):

term = $('#term_demo').terminal(function(command, term) {
    if (command !== '') {
        var result = window.eval("(" + command + ")");
        if (result !== undefined) {
            term.echo(String(result));
        }
    } else {
       term.echo('');
    }
}, {
    greetings: 'Javascript Interpreter',
    name: 'js_demo',
    height: 200,
    prompt: 'js> '
});

Demo

but it don’t work when I execute function foo() { ... } the foo is not defined I need to use foo = function() { ... }. eval act like executed within (function() { <code> })(). Can it be there more complicated code that will not work too?

Is it possible to create a JavaScript interpreter using simple code without use the of js.js, that will work the same as browser console?

  • 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-06T08:45:31+00:00Added an answer on June 6, 2026 at 8:45 am

    I’ve created a bookmarklet which appends a kind of REPL in a page, designed for the major five browsers (Chrome 1+, IE 6+, Firefox 1+, Safari 3+, Opera 9+Can’t remember the exacte version).
    The core component, which evaluates the code is posted below, slightly modified + annotated.

    /**
     * Evaluates some code in the global scope.
     * @param String code: Code to eval
     * @return Stringified result, prefixed with 'E:' if error.
     */
    function globalEval(/*string*/ code) {
        var win = window, r, raw;
        try {
            if (win.execScript) { // eval in IE sucks, so use execScript instead
                r = win.$_$_$globalEval$_$_$ = {c:code};
                win.execScript('try{$_$_$globalEval$_$_$.r=eval($_$_$globalEval$_$_$.c);}catch(e){$_$_$globalEval$_$_$.e=e}');
                // /*Optional clean-up:*/ delete win.$_$_$globalEval$_$_$;
                if (r.e) throw r.e; // Error
                raw = r.r;
            } else {
                raw = win.eval(code);
            }
            r = '' + raw; // Stringify in the try-block
                          // It is possible that an error is thrown
                          // for example, for the following code: ({toString:1})
        } catch(err) {
            r = (err + ''); // Convert error to string
            // IE: If found, "[object" will be at index zero, which is falsy
            if (!r.indexOf('[object')) r = err.message;
            // r = 
            r = 'E:' + (raw=r);
        } finally {
            // raw = unmodified result (or Error instance)
            // FOR THIS EXAMPLE, raw is not used, and string r is returned
            return /*string*/ r;
        }
    }
    

    I’ve implemented the functionality in a form, which contains several controls including an input+output textarea.

    Note: The code is evaluated in the global context. And such, any variables in code will be leaked to the global scope. For an interpreter, you could use an iframe to create a new scope (and modify var win in my function).

    var win = frames['name_of_frame'], ... // or
    var win = frame_DOM_element.contentWindow, ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is (should be) a simple question. I'd like to create a component like
This should be simple, but the answer is eluding me. If I've got a
I made a simple javascript class like this: function Horse() { this.color = 'brown';
I am trying to create a simple JavaScript function. When someone inserts a number
I'm trying to decide whether I should create a simple StackPanel with Buttons on
I am trying to create a very simple antlr grammar file which should parse
This should be simple, but I'm getting confused. I have a parent/child tables -
This should be simple task but i am not been able to find the
Using ICanHaz.js is really nice - it's dead simple to create, load and render
I'm trying to create a simple file upload form for my website. I'm using

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.