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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:44:40+00:00 2026-05-26T09:44:40+00:00

I send the variable newUser from options.html to background.html with chrome.extension.getBackgroundPage() like this document.getElementById(save).addEventListener(

  • 0

I send the variable newUser from options.html to background.html with chrome.extension.getBackgroundPage() like this

document.getElementById("save").addEventListener(
    "click", function ()
    {
        var newUser = document.getElementById("getEmail").value;
        var bkg = chrome.extension.getBackgroundPage();
        bkg.saveNewUser(newUser);
        console.log("newUser " + newUser);
    } , false)

In background.html I have

function saveNewUser (newUser)
{
    newUser = newUser; //this should be global?
    console.log("newUser from options page " + newUser);
}

console.log("newUser from options page " + newUser);

but newUser is local to the function. My understanding is that if a variable is saved inside a function without the keyword var it should be global. But in this case it is not. The console.log outside the function throws Uncaught ReferenceError: newUser is not defined error.

What am I doing wrong and how can I fix the scope to use newUser outside the function?

Thanks.

Edit

I tried the following in background.html but I still get newUser undefined error:

var extension_user

function saveNewUser (newUser)
{
    extension_user = newUser; //this should be global
    console.log("extension_user from options page " + extension_user);
}

console.log("extension_user from options page " + extension_user);

Update

I changed both pages to reflect the answers and comments but still outside the function the variable is undefined. What am I doing wrong?

options.html

document.getElementById("save").addEventListener(
    "click", function ()
    {
        var newUserEmail = document.getElementById("getEmail").value;
        var bkg = chrome.extension.getBackgroundPage();
        bkg.saveNewUser(newUserEmail);
        console.log("newUserEmail " + newUserEmail);
    } , false)

background.html

var newUser

function saveNewUser (newUserEmail)
{
    window.newUser = newUserEmail; //this should be global
    console.log("newUser from options page inside function" + newUser);
}

console.log("newUser from options page " + newUser);
  • 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-26T09:44:41+00:00Added an answer on May 26, 2026 at 9:44 am

    The newUser variable does not exist in the global scope until the function saveNewUser is called. Thus it gives a reference error. Just declare the variable newUser in the global scope as var newUser;. That should solve the problem. Always use the var keyword when declaring variables. It’s a good programming practice. Never declare global variables within local scopes. You already know why.

    Edits:

    Here’s the code to clarify my answer:

    var newUser; // new user declared as a global
    
    function saveNewUser (newuser)
    {
        newUser = newuser; // this should be global?
        console.log("newUser from options page " + newUser);
    }
    
    console.log("newUser from options page " + newUser); // no more reference error
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to send a variable to another function? like for this
I'm trying to send an ActionSheet a variable from a button. I can't use
I'm trying to send information from a form and a hidden email variable (from
I am trying to send a variable from a Javascript to a php script
I am trying to send a variable to shell script from jsp. i.e txtstr=xxx
I'm trying to figure out how to send a variable from the controller's view
How I can send rowNumber variable to dataSource php file in this code ?
Id like to send a variable to the global.asax, but when I debug the
I'm trying to send a variable from my main activity to a widget. I
I want to send a variable from my php page to my jquery 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.