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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:18:33+00:00 2026-06-05T08:18:33+00:00

This is really for informational and learning purposes while learning more about JavaScript and

  • 0

This is really for informational and learning purposes while learning more about JavaScript and CSS. I have a local browser index page that I wanted to rotate the background image onload. After looking around and playing with different solutions, I settled on this for the basic rotate functionality:

<html>
<head>
<script language="javascript" type="text/javascript">
function rotate()
{
   var imgArray = new Array("img1.jpg", "img2.jpg", "img3.jpg");
   var aImg = Math.floor(Math.random()*imgArray.length);
   var img = imgArray[aImg];
   document.body.style.background = "url(" + img + ") no-repeat";
   document.body.style.backgroundSize = "cover";
}
</script>
</head>
<body onload="rotate()">
</body>
</html>

During the process, before I just set the backgroundSizeas cover to fill the window, I was playing with the idea of resizing the images before setting them as the background image after they are selected from the array.

I have done a lot of searching, and the only real working solutions I have found rely on selecting the element by ID, but that also requires that the image has an ID associated, such as in the IMG property in the HTML code. Here the image is selected and set in the JavaScript with CSS.

I have tried setting the image dimensions with img.width / img.height and img.style.width / img.style.height, as well as a few other random solutions I have come across, but whenever I try to change these the image either does not change or it does not show at all.

function rotate()
{
   var imgArray = new Array("img1.jpg", "img2.jpg", "img3.jpg");
   var aImg = Math.floor(Math.random()*imgArray.length);
   var img = imgArray[aImg];
   image = rsize(img);
   document.body.style.background = "url(" + image + ") no-repeat";
   document.body.style.backgroundSize = "cover";
}
function rsize(image)
{
   image.style.width = "300px";
   image.style.height = "300px";
   return image;
}

I know I am probably doing something wrong here. Is there a way, in this circumstance, that I can resize these images? Or is there a better way to construct this?

Thanks in advance.

  • 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-05T08:18:34+00:00Added an answer on June 5, 2026 at 8:18 am

    You must set image.style.width and image.style.height on an actual image DOM object, not on the URL as you are currently trying to do.

    As an image object is not used for background images, you can’t really directly do what you’re trying to do for a background image.

    You could use the CSS background-size property, but that is fairly new and is not supported in versions of IE prior to IE9. If you were using that, you would set the actual size for that, not "cover".

    You could also use an actual DOM image and then present that DOM image as centered in your page if that’s what you’re really trying to do.

    For example, here’s how you create a DOM image object, assign it a URL, set it’s size and insert it into your page:

    var imgArray = new Array("img1.jpg", "img2.jpg", "img3.jpg");
    var aImg = Math.floor(Math.random()*imgArray.length);
    var imgURL = imgArray[aImg];
    var img = new Image();
    img.src = imgURL; 
    img.style.width = "300px";
    img.style.height = "300px";
    img.id = "centeredImage";
    document.body.appendChild(img);
    

    You could then use CSS to position is in the center of your page if you wanted.

    Working demo here: http://jsfiddle.net/jfriend00/jqMtV/

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

Sidebar

Related Questions

I have this really simple query. I am unable to figure out why my
I only began learning about web development 3 weeks ago and have grasped html,
this is a really basic question. I've been learning C++ and thus far I
While I was learning threading memory barrier (fences) seems really not easy to understand,
After learning a bit of how LLVM work I'm really excited about how portable
I just started studying programming about 6 months ago and I have really been
i want to fetch information from the database using objects. i really like this
This seems like a really simple question but I can't seem to find information
This really seems like a bug to me, but perhaps some databinding gurus can
This really shouldn't be hard, I just can't figure out how to do it.

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.