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

The Archive Base Latest Questions

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

I have a situation in which I have to use eval in Javascript. Yes,

  • 0

I have a situation in which I have to use eval in Javascript. Yes, I know it is evil, but in this case it is required, because two requisites:

  1. The Javascript code is big.
  2. It has to be evaluated (and downloaded) only in a few situations, so I don’t want the script to be downloaded always, but on demand.

With these requisites, I have written an XMLHttpRequest to request the code (600+K), and I want to eval that code to take place immediately. As I’ll show later, the script contains just data to be inserted in two variables. The problem is that evaluating this javascript fails always with syntax error. If I put the code verbatim (without eval), the code executes fine. This is the minimal example. I have a search.js file like this:

// search.js for functional mind. Generated statically
// from the set of posts.
posts = { 'postlist' : ['<a href="XXX">YYY</a>', 'other link...' ] };
posts_for_word = { 'word1' : [0,1], 'word2' : [Z,K] }; 

The last variable, posts_for_word includes indexes of posts containing that word. The problem comes when I evaluate that Javascript inside eval:

// global environment
var posts = {}; // Initial value, empty
var posts_for_word = {}; // same

function() {
var request = // create XMLHTTPREQUEST request
var response = // obtain response (the javascript)
eval( response ); // <- syntax error
// access posts or posts_for_word
posts.postlist[ posts_for_word['word1'][0] ]
....
}

The problems:

  1. When I evaluate the javascript shown above with eval, firefox tells me that it is bad formed in the first line.
  2. I then removed the comments. I supposed that eval would evaluate it as if it was a normal Javascript program, but it doesn’t seem to accept comments (why?).
  3. After removing the comments, the code works, but still the Javascript console of Firefox tells me that there is a syntax error in the first line (that defining the JSON first variable). Why?
  • 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-26T03:25:06+00:00Added an answer on May 26, 2026 at 3:25 am

    You shouldn’t use AJAX and eval() to make a script happen on your website, instead, create a <script> element with a link to the file and append it to the body:

    (function() {
        var jsCode = document.createElement('script');
        jsCode.setAttribute('src', 'http://localhost/tests/doStuff.js');
        document.body.appendChild(jsCode);
    }());
    

    (This code is actually taken directly from a bookmarklet of mine, and it does the trick nicely.)

    As for the variable scope, if you declare them as var variable in your inner file, it would override the file scope and become global.


    However, should you require a synchronous load of a script, you will have to (despite the fact it is considered bad practice) to use a synchronous AJAX call. How to do that was already written before me, Dynamically loading JavaScript synchronously

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

Sidebar

Related Questions

I have a situation which I have solved in two different ways, but was
I have the situation where i use GIS software which stores the information about
I have come across a situation (which I think is weird but is possibly
I have this situation which is rather frustrating... I have some user controls where
I have a situation where I need to use some strings temporarily but I've
not so clear about this feature. the situation is we have API which has
I have this situation, I use Netbeans 6.8 with recommended MinGW+msys configuration, and compiling
I have this situation, I use Netbeans 6.8 with recommended MinGW+msys configuration, and compiling
I have situation in which I read a record from a database. And if
I have a situation in which a managed DLL calls some unmanaged DLL. I

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.