Home Web Front-end JS Tutorial Confusion about the difference between img objects in IE and Firefox_Basic knowledge

Confusion about the difference between img objects in IE and Firefox_Basic knowledge

May 16, 2016 pm 07:22 PM


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.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

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

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

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

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

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

How do I debug JavaScript code effectively using browser developer tools? How do I debug JavaScript code effectively using browser developer tools? Mar 18, 2025 pm 03:16 PM

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

10 Ways to Instantly Increase Your jQuery Performance 10 Ways to Instantly Increase Your jQuery Performance Mar 11, 2025 am 12:15 AM

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

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

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...

Using Passport With Sequelize and MySQL Using Passport With Sequelize and MySQL Mar 11, 2025 am 11:04 AM

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

How to Build a Simple jQuery Slider How to Build a Simple jQuery Slider Mar 11, 2025 am 12:19 AM

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

See all articles