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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:21:13+00:00 2026-06-04T08:21:13+00:00

I am trying to code a chrome extension, and I have a background.html with

  • 0

I am trying to code a chrome extension, and I have a background.html with this code:

var x = "test";

function tabChanged(id, info, tab){
    if(info.status == 'complete'){
        chrome.tabs.executeScript(id, {code:"try{alert(x);}catch(e){alert(e);}"}, null);
    }
}

chrome.tabs.onUpdated.addListener(tabChanged);
chrome.tabs.getAllInWindow(null,function(tabs){
    for(var index=0; index < tabs.length; index++){
        chrome.tabs.executeScript(tabs[index].id, {code:"try{alert(x);}catch(e){alert(e);}"}, null);
    }
});

But variable “x” is always undefined inside executeScript.

How can I get/set x from executeScript? Without using messaging.

  • 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-04T08:21:15+00:00Added an answer on June 4, 2026 at 8:21 am

    Content scripts executes in context of web page. See Content Scripts section in Chrome docs for more information.

    If you want to pass string variable from background page to chrome.tabs.executeScript you must do something like this:

    var x = "test";
    chrome.tabs.executeScript(id, {code:"var x = '"+x+"'; try{alert(x);}catch(e){alert(e);}"},null);
    

    If you want to modify variable, you have only one way – messaging:

    var x = 1;
    console.log('x=' + x);
    
    chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
        console.log(request);
        if(request.command == 'setVar') {
            window[request.name] = request.data;
        }
    });
    
    chrome.browserAction.onClicked.addListener(function() {
        var code = "chrome.extension.sendRequest({command: 'setVar', name: 'x', data: 2});";
        chrome.tabs.executeScript(null, {code:code});
        window.setTimeout(function(){console.log('x=' + x);}, 1000);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this code trying to print an image in an html using session
I have a Chrome Extension I'm trying to add features with an enable/disable option.
I'm trying to create a chrome extension and im doing this by parsing an
I have this Chrome Extension, it adds a poke all button under your pokes.
I have this code // // ==UserScript== // @name test // @description test //
I have a Chrome extension which is injecting some code into a web page
I started trying to write chrome extension and I'm having problem with simple function:
I am trying to attempt my first Google Chrome Extension and have a question.
I am trying to modify table's insertRow function from Chrome extension. oldInsRowFunc = document.getElementById(id).insertRow;
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.