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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:02:13+00:00 2026-05-23T13:02:13+00:00

I use this javascript to resize, to a max of 800px, a big (5Mb)

  • 0

I use this javascript to resize, to a max of 800px, a big (5Mb) image on a page:

 <script type="text/javascript">
 $(document).ready(function () {
     $('.detailImg').each(function () {
         var maxWidth = 800; // Max width for the image
         var maxHeight = 800;    // Max height for the image
         var ratio = 0;  // Used for aspect ratio
         var width = $(this).width();    // Current image width
         var height = $(this).height();  // Current image height

         // Check if the current width is larger than the max
         if (width > maxWidth) {
             ratio = maxWidth / width;   // get ratio for scaling image
             $(this).css("width", maxWidth); // Set new width
             $(this).css("height", height * ratio);  // Scale height based on ratio
             height = height * ratio;    // Reset height to match scaled image
             width = width * ratio;    // Reset width to match scaled image
         }

         // Check if current height is larger than max
         if (height > maxHeight) {
             ratio = maxHeight / height; // get ratio for scaling image
             $(this).css("height", maxHeight);   // Set new height
             $(this).css("width", width * ratio);    // Scale width based on ratio
             width = width * ratio;    // Reset width to match scaled image
         }
     });
 });

When page is loaded the image is not resized. No errors on FireBug console.
If I try to refresh the page with F5, the image get resized!
If I try Ctrl + F5, images return at original size!

Where is the problem? Why this javascript only works when the page is refreshed?

  • 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-23T13:02:14+00:00Added an answer on May 23, 2026 at 1:02 pm

    Your images aren’t loaded when your document.ready() runs. document.ready() fires on load of the DOM, but at that point the images may still be loading — especially if they’re 5MB! Because of this, the image height and width aren’t available at the point your script is running. I’d guess it’s working on F5 because the images are still cached, so are then loaded fast enough for their dimensions to be known when your script runs. Ctrl–F5 clears the image cache, so after that, you’re back where you started.

    As I noted in my comment, using the load() event of an image may help, but even then, sometimes it’s not triggered properly. For example, some browsers will never fire the load() event for an image that’s already in the browser cache (as, technically, it’s not been loaded…)

    There are a few different workarounds for this doing the rounds — Paul Irish’s plugin might be useful; include the plugin code and then use something like:

     $('.detailImg',this).imagesLoaded(function() {
       // Your existing function code
     });
    

    Alternatively, if you know the image size in advance, just specify it in your img element, e.g. <img src="whatever" height="1200" width="1600" alt="whatever" />. That way jQuery will be able to tell the width and height whether the image is loaded or not.

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

Sidebar

Related Questions

i use this javascript syntax for validating a checkbox... alert(document.getElementById(ctl00_ContentPlaceHolder1_Chkreg).checked); if (document.getElementById(ctl00_ContentPlaceHolder1_Chkreg).checked == false)
During development, I use this to load the latest jQuery: <script src=http://www.google.com/jsapi></script> <script type=text/javascript>
i am using the code found here jQuery - dynamic div height <script type='text/javascript'>
In order to put e-mail addresses on my sites, I use this Javascript :
Is it possible to get the value from a JavaScript variable and use this
I already started this in javascript so I don't want to use jquery but
I am trying to get a JavaScript object to use the this assignments of
I use an externel javascript file and i have this, function getdropdownvalue() { alert($(#<%=DLState.ClientID%>));
I am trying to use an HTML form and javascript (i mention this, because
I'd like to use JavaScript to calculate the width of a string. Is this

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.