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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:04:34+00:00 2026-05-27T02:04:34+00:00

I have a javascript variable: var foo='<script type=text/javascript>alert(Hello World);<\/script>’ The variable is inserted with

  • 0

I have a javascript variable:

var foo='<script type="text/javascript">alert("Hello World");<\/script>'  

The variable is inserted with element.innerHTML=foo; after an event occurs on the page, about 10 seconds after the page is loaded.

Is there a way to execute the ‘alert’ function right after the insertion?

  • 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-27T02:04:35+00:00Added an answer on May 27, 2026 at 2:04 am

    If you absolutely, positively have to take JavaScript code that’s in a string and execute it, you basically have to use eval or an eval-like mechanism. In some years of JavaScript programming, I’ve never had to resort to it, and I do suggest that you look at whether there’s another way to achieve your actual overall goal.

    So here, you’d strip off the script tag stuff and just eval the code, e.g.:

    var script = foo.replace(/^<script[^>]*>/, "").replace(/<\/script>$/, "");
    eval(script);
    // Or window.evalInGlobalScope(script); // -- See below
    

    Obviously you have to be sure you trust the source of the string, since you’re executing the code therein.

    eval is a slippery beast and plays very odd games with context and scope. If you need something that looks more like what you’d get if you did add a script tag to the page, here’s a function that does that cross-browser (from my answer to this other question here on Stack Overflow):

    window.evalInGlobalScope = (function() {
        var fname, scr;
    
        // Get a unique function name
        do {
            fname = "__eval_in_global_test_" + Math.floor(Math.random() * 100000);
        }
        while (typeof window[fname] !== 'undefined');
    
        // Create test script
        scr = "function " + fname + "() { }";
    
        // Return the first function that works:
        return test(evalInGlobalScope_execScript) ||
               test(evalInGlobalScope_windowEval) ||
               test(evalInGlobalScope_theHardWay) ||
               evalInGlobalScope_fail;
    
        function test(f) {
            try {
                f(scr);
                if (typeof window[fname] === 'function') {
                    return f;
                }
            }
            catch (e) {
                return false;
            }
            finally {
                try { delete window[fname]; } catch (e) { window[fname] = undefined; }
            }
        }
        function evalInGlobalScope_execScript(str) {
            window.execScript(str);
        }
        function evalInGlobalScope_windowEval(str) {
            window.eval(str);
        }
        function evalInGlobalScope_theHardWay(str) {
            var parent, script, d = document;
    
            parent = d.body || d.documentElement || d.getElementsByTagName('head')[0];
            if (parent) {
                script = d.createElement('script');
                script.appendChild(d.createTextNode(str));
                parent.appendChild(script);
            }
        }
        function evalInGlobalScope_fail() {
            throw "evalInGlobalScope: Unable to determine how to do global eval in this environment";
        }
    })();
    

    Live example using the above

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

Sidebar

Related Questions

I have javascript string variable with var sttr=We prefer questions that can be answered
I have a paragraph of text in a javascript variable called 'input_content' and that
I have a select control, and in a javascript variable I have a text
In Javascript, I have a variable that is set to a block of text
I have a JavaScript variable: var s = <html><head><style>body{a:b;c:d}</style></head><body></body></html>; First, I am extracting the
if I have a javascript variable: var page = <!DOCTYPE HTML PUBLIC \-//W3C//DTD HTML
I have javascript variable obj which represents an element in the DOM and I
I have a variable var i = "my*text" I want to split it using
I have a Javascript variable whatToRefresh which is defined in this way: var whatToRefresh
I have a javascript file called pendingAjaxCallsCounter.js with a variable var pendingAjaxCalls which is

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.