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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:05:12+00:00 2026-06-10T15:05:12+00:00

I have a basic Google Chrome Extension I am working on, and having a

  • 0

I have a basic Google Chrome Extension I am working on, and having a problem getting a value from localStorage. I am using sendMessage in the content script to ask for the variable from the background page. When I get the response, the variable is always “undefined”, unless I put something like an alert() after the response…then the variable contains my value. Can you help me to figure out why this is and how to do it correctly? I have made the code for this post and it is below. Thank you very much!


manifest.json

{
  "name": "Test Extension",
  "version": "1.0",
  "manifest_version": 2,
  "description": "A test extension.",
  "background": {
    "scripts": ["background.js"]
  },
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["content.js"]
    }
  ],
 "permissions": [
    "tabs", "<all_urls>"
  ]
}

content.js

alert('content');

var color;

chrome.extension.sendMessage({greeting:"color"},function(response){color = response.farewell;});

// alert('pause'); // uncommenting this line lets me get the value...why?

alert('content - ' + color);

background.js

alert('background');

localStorage["color"] = "lightblue";

var color = localStorage["color"];

alert('background - ' + color);

chrome.extension.onMessage.addListener(function(request,sender,sendResponse)
{

if (request.greeting == "color") {sendResponse({farewell:color});}

});

Thank you very much for any help! 🙂

  • 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-10T15:05:13+00:00Added an answer on June 10, 2026 at 3:05 pm

    Because the response is in the callback function. When you have this:

    chrome.extension.sendMessage({greeting:"color"},function(response){color = response.farewell;});
    
    alert('content - ' + color);
    

    The browser executes sendMessage and then IMMEDIATELY goes ot the next line, the alert. The callback in sendMessage is fired after. The alert ‘fixes’ it because you are basically pausing the browser. If you want to use color as a variable, you can only use it inside the callback: function(response).

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

Sidebar

Related Questions

Im new on Google Chrome Extensions coding, and i have some basic questions. I
I have a basic problem but Google is not yielding many results for me
I've created a basic extension for Google Chrome to change the behavior of which
I have read some of the basic Google Apps Script documentation/tutorials. I have not
I have a very basic html, supposingly <html> <body> <iframe src=http://www.google.com> <p>Your browser does
I'm trying to develop a Google Chrome extension. When the user clicks the extension
I have a bit of a problem with my Google Maps application. So I'm
I've been unable to get the basic Google maps example working on my site.
I have basic single page website with the normal Google Analytic code: var _gaq
I'm working with python, but I have a basic understanding of packaging with C.

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.