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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:42:11+00:00 2026-06-10T18:42:11+00:00

I have another javascript/jQuery’s variables scope questions to ask… Say I declared a object

  • 0

I have another javascript/jQuery’s variables scope questions to ask…

Say I declared a object named Container. In which there is a variables called myimage which will read a address from some xml file.

 Container = function()
 {
   var myimage;
 }

 Container.prototype.readXML = function()
 {
   $.get("assest/xml/images.xml",function(xml)
{
    //Read in URL path from XML file, and store them into memeber variables
    myimage = $(xml).find('background').text();
    //Apply background image into body, and apply css styple into it
    $("body").css('background-image','url(' + myimage + ')');
            //This alert will work
            alert(myimage);
});
            //This alert gives not defined variable
    alert(myimage);
  }

Please look at the two alert section. It seems this variable I defined in the Container object, can only work inside that readXML function. but not out. I can’t understand why this happend.

I do use some other notation, like declare with
this.myimage
and access it by change name of this to self before execute the $.get function
var self= this;

But it get worse. Sometimes it even can’t be reached anymore inside the get function.

Could you help me with this? My final goal is an array in that object and read bunch of data from XML than display them into HTML. If the variables I set in the Object can’t be reached, there is no way I can do that.

Thank you!!

  • 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-10T18:42:12+00:00Added an answer on June 10, 2026 at 6:42 pm
     Container = function()
     {
       var myimage;
     }
    

    should most likely be defined as below. More importantly, $.get is asynchronous so you cannot assume it finishes each line of code in the order it’s written.

     var Container = function()
     {
       this.myimage = '';
     }
    
    Container.prototype.readXML = function(callback) {
        $.get("assest/xml/images.xml", function(xml) {
            //Read in URL path from XML file, and store them into memeber variables
            this.myimage = $(xml).find('background').text();
            //Apply background image into body, and apply css styple into it
            $("body").css('background-image', 'url(' + this.myimage + ')');
            //This alert will work
            callback(this.myimage);
        });
    }​
    
    var instance = new Container();
    instance.readXML(function (copy) {
        alert(copy);
    });​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Javascript/JQuery I have a need to copy one elements CSS style object to
I have declared a function for showing a dialog box in jQuery <script type=text/javascript>
Can you setup a variable for javascript in one page and have another page
I have Javascript that opens another window and registers a click handler for all
I have this Javascript code I inherited from another developer. I am very new
I have a javascript link that references another .js file. I've been trying to
I have some javascript that goes out and fetches a javascript class on another
Is there a way (using javascript/jQuery) to do following: On my page, I have
How to identify browser tab closing using JavaScript or jQuery? I have to give
I have a simple javascript (JQuery based) method that does some css magic to

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.