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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:31:35+00:00 2026-06-01T04:31:35+00:00

Is it possible to inject a javascript file into the DOM and immediately execute

  • 0

Is it possible to inject a javascript file into the DOM and immediately execute it ?
I wish to invoke javascript functions within the page/DOM. A single content script will not work because of the isolated worlds. A background page is required to use chrome.tabs.executeScript().

Simple Example:
DOM javascript

function sayHello(){
  alert('Hello World');
}

Javascript file to inject

console.log('Injection complete. Now calling DOM script.');
sayHello();
  • 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-01T04:31:36+00:00Added an answer on June 1, 2026 at 4:31 am

    Here’s my two favorite ways…

    // Executing an anonymous script
    function exec(fn) {
       var script = document.createElement('script');
       script.setAttribute("type", "application/javascript");
       script.textContent = '(' + fn + ')();';
       document.documentElement.appendChild(script); // run the script
       document.documentElement.removeChild(script); // clean up
    }
    
    script = function() {
    //sayHello();
    alert('hello');
    }
    
    exec(script);
    
    // Append a script from a file in your extension
    function appendScript(scriptFile) {
       var script = document.createElement('script');
       script.setAttribute("type", "application/javascript");
       script.setAttribute("src", chrome.extension.getURL(scriptFile));
       document.documentElement.appendChild(script); // run the script
    }
    
    appendScript('someFile.js');
    

    Also chrome.tabs.executeScript() can be used from a browser/page action popup and the above code works in a content script aswell.

    EDIT
    Thanks to comments by @renocor, here’s a variation of the first method that allows you to send arguments to the injected function….

    function exec(fn) {
        var args = '';
        if (arguments.length > 1) {
            for (var i = 1, end = arguments.length - 2; i <= end; i++) {
                args += typeof arguments[i]=='function' ? arguments[i] : JSON.stringify(arguments[i]) + ', ';
            }
            args += typeof arguments[i]=='function' ? arguments[arguments.length - 1] : JSON.stringify(arguments[arguments.length - 1]);
        }
        var script = document.createElement('script');
        script.setAttribute("type", "application/javascript");
        script.textContent = '(' + fn + ')(' + args + ');';
        document.documentElement.appendChild(script); // run the script
        document.documentElement.removeChild(script); // clean up
    }
    
    script = function(what, huh, nah, yeah) {
        console.debug(arguments);
        console.debug('what=', what);
        console.debug('huh=', huh);
        console.debug('nah=', nah);
        console.debug('yeah=', yeah);
        if (typeof yeah=='function') yeah();
    }
    
    exec(script, 'meh', ['bleh'], {
        a: {
            b: 0
        }
    }, function(){
        alert('hi');
    });
    
    console.debug('No arguments');
    
    exec(script);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to inject a DLL file into a process such as explorer
Is it at all possible to inject a request into IIS for a page,
Is is possible to inject IPrincipal using Castle Windsor into my asp.net mvc controller.
Is it possible to inject an interface into an existing 3rd party class that
Is it possible to inject ejb's into pojo's using the @EJB annotation? If it
I was wondering if its possible to inject a thread into a remote app
Is it possible to have Spring inject the JPA entityManager object into my DAO
I would like to know if it is possible to inject some javascript code
Within a Chrome extension, how can I inject items into the context menus of
Is it possible to inject Spring beans into an RestEasy @Path class? I managed

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.