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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:05:27+00:00 2026-06-01T02:05:27+00:00

I am trying to get my Chrome Extension to run the function init() whenever

  • 0

I am trying to get my Chrome Extension to run the function init() whenever a new page is loaded, but I am having trouble trying to understand how to do this. From what I understand, I need to do the following in background.html:

  1. Use chrome.tabs.onUpdated.addListener() to check when the page is
    changed
  2. Use chrome.tabs.executeScript to run a script.

This is the code I have:

//background.html
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    chrome.tabs.executeScript(null, {code:"init();"});
});

//script.js
function init() {
    alert("It works!");
}

I am also wondering if the init() function will have access to my other functions located in other JS files?

  • 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-01T02:05:29+00:00Added an answer on June 1, 2026 at 2:05 am

    JavaScript code in Chrome extensions can be divided in the following groups:

    • Extension code – Full access to all permitted chrome.* APIs.
      This includes the background page, and all pages which have direct access to it via chrome.extension.getBackgroundPage(), such as the browser pop-ups.

    • Content scripts (via the manifest file or chrome.tabs.executeScript) – Partial access to some of the chrome APIs, full access to the page’s DOM (not to any of the window objects, including frames).
      Content scripts run in a scope between the extension and the page. The global window object of a Content script is distinct from the page/extension’s global namespace.

    • Injected scripts (via this method in a Content script) – Full access to all properties in the page. No access to any of the chrome.* APIs.
      Injected scripts behave as if they were included by the page itself, and are not connected to the extension in any way. See this post to learn more information on the various injection methods.

    To send a message from the injected script to the content script, events have to be used. See this answer for an example. Note: Message transported within an extension from one context to another are automatically (JSON)-serialised and parsed.


    In your case, the code in the background page (chrome.tabs.onUpdated) is likely called before the content script script.js is evaluated. So, you’ll get a ReferenceError, because init is not .

    Also, when you use chrome.tabs.onUpdated, make sure that you test whether the page is fully loaded, because the event fires twice: Before load, and on finish:

    //background.html
    chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
        if (changeInfo.status == 'complete') {
            // Execute some script when the page is fully (DOM) ready
            chrome.tabs.executeScript(null, {code:"init();"});
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

google-chrome-extension I´m having trouble with an extension I´m trying to build. The thing is
I've been trying to get my Chrome extension to auto update but it doesn't
I'm trying to create a chrome extension which scans a local directory for new
I'm trying my hands at a simple Chrome Extension, but am running into a
I am trying to create a chrome extension using HTML5 WebSockets and can't get
I am trying to use the google tasks api in a chrome extension, but
I'm making a google chrome extension and trying to get reference of a local
I'm trying to get my Chrome extension working with the Google Calendar API. However,
I am having trouble accessing the document in my firefox extension. I am trying
I'm trying to get HTML5's audio tag to work in Chrome. The following code

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.