


Confusion about the difference between img objects in IE and Firefox_Basic knowledge
I encountered some disgusting problems when debugging js, so I made a test program and put it online for everyone to help me test it. See the posthttp://vchelp.net/cndevforum/subject_view.asp?page=-1&subject_id=165791
I will give an explanation about the test below:
The reason is that I want to make such a web page: after the user uploads a picture, if the picture is larger than 500 pixels, the picture will be displayed on the client Shrink to 500 pixels size. But I don't want users to see this resizing process. So I want to hide the image first, resize it after the entire web page is downloaded, and then display the adjusted image.
So I first set the style="display:none" of the img tag, and then obtained the original image size in window.onload and adjusted it.
It turns out that under firefox, an image width of disolay=none and height are the actual sizes of the original image, but under IE they are both 0
, so I thought of a safe way to create an image object, then assign a value to src, and then read the original image size information:
var oImg = new Image();
oImg.src = docuent.getElementById("c010_jpg").src;
//Read the width and height of oImg immediately
alert([oImg.width, oImg .height]);
The result was found in the IE test that the above code will output "0,0"
I suspect this means that when IE parses a display:none img tag, it does not download this Picture, so after the above code assigns a value to oImg.src, IE needs to download the picture from the target address. Of course, this process is an asynchronous process
and under firefox, the above code will output the correct information, which shows that firefox parses When display:none is used for a picture, the picture has already been downloaded. When assigning a value to oImg.src in the future, it will be obtained directly from the cache, so it is fast
Thinking of this, I had to use a more complicated and safer method:
var oImg = new Image();
oImg.onload = function (){alert([oImg.width, oImg.height]);}
oImg.src = docuent.getElementById("c010_jpg").src;
//When src is After loading, there is no way to output the width and height of oImg
using events and callback functions. Handling this asynchronous process makes the program structure look ugly.
In addition, the readyState and complete attributes of HTMLImageElement were not found in w3c (http://www.w3.org/TR/REC-DOM-Level-1/idl-definitions.html).
I found that firefox implements the complete attribute, while IE implements the complete attribute and the readyState attribute
But the two definitions of the attributes seem to be different:
firefox: An image has been downloaded, complete The attribute is true, if the download is not completed, it is false
ie: If an image has not been downloaded, the readyState attribute is uninitialized, and the complete attribute is false. When the download is completed, readyState is complete, and if the image is not displayed at this time , complete is false, and this attribute becomes true after display (display:block)
I didn’t expect that a simple function would be so laborious. The browser compatibility problem is difficult to solve smoothly, especially many details are very wasteful. Time, I hope others will consider solving these problems from server-side scripts when they encounter these problems. This bypasses complex testing for browser compatibility.
In addition, I am also very confused as to why most IEs in reality are not asynchronous for the onload event, and only a few gay IEs are asynchronous for this event.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

This article outlines ten simple steps to significantly boost your script's performance. These techniques are straightforward and applicable to all skill levels. Stay Updated: Utilize a package manager like NPM with a bundler such as Vite to ensure

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Sequelize is a promise-based Node.js ORM. It can be used with PostgreSQL, MySQL, MariaDB, SQLite, and MSSQL. In this tutorial, we will be implementing authentication for users of a web app. And we will use Passport, the popular authentication middlew

This article will guide you to create a simple picture carousel using the jQuery library. We will use the bxSlider library, which is built on jQuery and provides many configuration options to set up the carousel. Nowadays, picture carousel has become a must-have feature on the website - one picture is better than a thousand words! After deciding to use the picture carousel, the next question is how to create it. First, you need to collect high-quality, high-resolution pictures. Next, you need to create a picture carousel using HTML and some JavaScript code. There are many libraries on the web that can help you create carousels in different ways. We will use the open source bxSlider library. The bxSlider library supports responsive design, so the carousel built with this library can be adapted to any
