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

  • Home
  • SEARCH
  • 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 3663076
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:29:20+00:00 2026-05-19T01:29:20+00:00

Are there any alternatives to using eval to immediatly run remote & trusted javascript

  • 0

Are there any alternatives to using eval to immediatly run remote & trusted javascript code.

function load(filePath) {
    var o = $.ajax({
        url: filePath,
        dataType: 'html',
        async: false 
    }); 

    eval(o.responseText);
}

load("somePath");
// run a function that relies on the code from o.responseText being loaded
doSomethingWithCode();

I’m aware that synchronous loading of javascript is adviced againts. But if there is no choice are there any cross browser alternatives for the use of eval above.

[Edit]

To clarify in more detail the code being loaded is a self executing function. Which needs to execute before doSomethingWidthCode. It’s also being loaded from the server on the same domain hence its trusted.

  • 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-19T01:29:21+00:00Added an answer on May 19, 2026 at 1:29 am

    Dynamic script text insertion is the only alternative to eval.

    var head    = document.getElementsByTagName('head')[0] || document.documentElement,
        nscr    = document.createElement('script');
    
        nscr.type           = 'text/javascript';
        nscr.textContent    = o.responseText;
        nscr.setAttribute('name', 'dynamically inserted');
        nscr.onload         = nscr.onreadystatechange = function() {
                  if( nscr.readyState ) {
                       if( nscr.readyState === 'complete' || scr.readyState === 'loaded' ) {
                          nscr.onreadystatechange = null;
                           doSomethingWithCode();
                  }
                  else {
                      doSomethingWithCode();
                  }
        };
    
        head.insertBefore(nscr, head.firstChild);
    

    Only thing to mention: textContent is not available in InternetExplorers. You would need to use .text instead there, so a little detection for that makes it cross-browser compatible.

    edit

    To have a syncronous loading dynamic script tag, you could add nscr.async = true;. Anyway, this only works in cutting edge browsers.

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

Sidebar

Related Questions

Are there any alternatives to IIS for Windows to run ASP.NET? CassiniEx seems pretty
I've been using eval in my code and I recently found out that there
Is there any alternatives to manipulate Microsoft Word document (2003 and above) without using
Are there any alternatives to The CMU Pronouncing Dictionary , commercial or open source?
Are there any alternatives to JSTL? One company I worked for 3 years ago
I was wondering if there are any alternatives to Microsoft's SQL Server Management Studio?
Does everyone just use XML in the message? Are there any good alternatives to
Are there any other ViewState alternatives? I've heard a lot Like Session, holding the
Using JSlint to validate my javascript. I am getting an error saying eval is
Are there any alternatives to the :not css3 selector that are compliant with IE8

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.