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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:07:40+00:00 2026-06-01T13:07:40+00:00

I preload load two different images at the top of my javascript file. var

  • 0

I preload load two different images at the top of my javascript file.

var imgColor = new Image();
var imgLoadedColor = false;
imgColor.src = 'color/progress.png';

imgColor.onload = function(){
  imgLoadedColor = true;
}   

var imgBlackWhite = new Image();
var imgLoadedColor = false;
imgBlackWhite.src = 'blackWhite/progress.png';

imgBlackWhite.onload = function(){
  imgLoadedColor = true;
}   

The string in this.options.type is either imgColor or imgBlackWhite.

When I try to pass the argument this.options.type to a function, I get an error because the value in this.options.type is a string, not an object. However if I pass the argument imgColor it loads the colored image and if I pass the argument imgBlackWhite it loads the black and white image because imgColor and imgBlackWhite are objects.

How do I create a reference to the objects imgColor and imgBlackWhite from the value of the string in this.options.type?

  • 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-01T13:07:41+00:00Added an answer on June 1, 2026 at 1:07 pm

    Why not just use an if statement?

    if (arg == "imgColor") return imgColor;
    else return imgBlackWhite;
    

    EDIT: you can also use new windowtypename to instantiate an object (per this thread: Instantiate a JavaScript Object Using a String to Define the Class Name). So, instead of the above:

    return new window[arg]();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my preload script: <script type=text/javascript> Image_1 = new Image(1,1); Image_1.src = images/sprites.png;
hi am using this snippet for preload image using javascript window.onload=function(){ heavyImage.src = images/qq-lookupbody-holder.png;
I have following code on my site: backgroundImages[bg_img_path_b]=new Image(); backgroundImages[bg_img_path_b].src = bg_img_path_b; backgroundImages[bg_img_path_b].loaded=loading; //jQuery(backgroundImages[lastImage]).unbind('load.backgroundImages');
I'm looking for a quick and easy way to preload images with JavaScript. I'm
I use the load function to preload Images before applying a hover effect. $(img).load(function(){
I have been using the following preload script to load all the images and
Currently I am using a pretty basic function to pre-load images: function preload(arrayOfImages) {
here i am giving the code to preload image by jquery. var _images =
How can you preload an entire web page using JavaScript so that I can
I'm trying to preload about 150 images and I want to be able 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.