How to make the browser (IE and Chrome) request im

2019-03-14 18:20发布

Note: If you are reading this for the fist time, you may jump directly to the UPDATE, since it addresses the issue more accurately.

So I got a web-page.

In the head I have a CSS background-image:

<style>
    #foo { background-image:url(foo.gif); }
</style>

At the bottom of the page I load my scripts:

<script src="jquery.js"></script>
<script src="analytics.js"></script>

Since the scripts are located at the bottom of the page, and the CSS at the top of the page, I assumed that browsers will load the image first. However, this seems not to be the case.

This is a screenshot from the Chrome Dev Tools:

http://www.vidasp.net/media/cssimg-vs-script.png

As you can see, the image loads after the scripts. (The vertical blue line is the page load DOMContentLoaded event. The huge 45ms gap is the time in which Chrome parses the jQuery source code.)

Now, my first question is:

Is this standard behavior in browsers? Do CSS background-images always load after all the scripts on the page?

If yes, how could I make sure that those images load before the scripts? Is there an easy and convenient solution to this problem?


UPDATE

I made a test case. This is the HTML source code:

<!DOCTYPE html>
<html>
<head>
    <style> body { background-image: url(image1.jpg) } </style>
</head>
<body>
    <div> <img src="image2.jpg"> </div>
    <script src="http://ajax.googleapis.com/ajax/.../jquery.min.js"></script>
    <script src="http://vidasp.net/js/tablesorter.js"></script>
</body>
</html>

As you can see, I have one CSS background-image, one regular image, and two scripts. And now the results:


INTERNET EXPLORER (9 beta)

http://www.vidasp.net/media/loadorder-results/ie2.png
http://www.vidasp.net/media/loadorder-results/ie1.png

Internet Explorer requests the regular image first, then the two scripts, and the CSS image last.


FIREFOX (3.6)

http://www.vidasp.net/media/loadorder-results/firefox2.png
http://www.vidasp.net/media/loadorder-results/firefox1.png

Firefox is doing it right. All resources are requested in the order in which they appear in the HTML source code.


CHROME (latest stable)

http://www.vidasp.net/media/loadorder-results/chrome2.png
http://www.vidasp.net/media/loadorder-results/chrome1.png

Chrome demonstrates the issue that made me write this question in the first place. The scripts are requested before the images.


OPERA (11)

http://www.vidasp.net/media/loadorder-results/opera1.png
http://www.vidasp.net/media/loadorder-results/opera2.png

Like Firefox, Opera is doing it right, too. :D


To sum up:

  • Firefox and Opera are requesting the resources as they appear in the source code.

Exceptions so this rule:

  • Internet explorer requests CSS background-images last
  • Chrome requests scripts before images even when the scripts appear later in the source code

Now that I laid out the issue, let me move on to my question:

How to make IE and Chrome request the images before the scripts?

5条回答
该账号已被封号
2楼-- · 2019-03-14 18:26

Most latest browsers to this kind of unpredictable parallel preloading of stuff these days, for performance reasons and basically ruining any chance of creating an order for loading components. This of course happens once the full DOM has been loaded. Same story as with JQuery lazy loading of images, which has been broken for a while now.

查看更多
劳资没心,怎么记你
3楼-- · 2019-03-14 18:31

A possible option is to load all those images that you need at the start of your script. See this TechRepublic article for more info.

查看更多
成全新的幸福
4楼-- · 2019-03-14 18:37

We solved this problem using .load inside the .ready jquery call

something like:

jQuery(document).ready(function($){
  jQuery('#my_container img').load(function($){
     /* SCRIPT */
  });
});
查看更多
放荡不羁爱自由
5楼-- · 2019-03-14 18:38

Consider that the browser can't do anything till it builds the DOM. So first it parses the whole page, THEN it loads the images (even if they're from the CSS).

You could load the images in DATA segments inline in the CSS or the page, that might speed those things up, or you could inject the jQuery reference after the page is loaded (say set a timer for 500 ms) but obviously that will affect usability to some extent.

Now, I'm pretty sure this is all implementation dependent, you could always find a browser that would load images as it came to them, but consider what it means to build a DOM and then to fill it in.


http://en.wikipedia.org/wiki/Data_URI_scheme

If SO doesn't strip it, there should be a red dot between here and the code :\

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGPC/xhBAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IAAAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFoZSBHSU1Q72QlbgAAAF1JREFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jqch9//q1uH4Tzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0vr4MkhoXe0rZigAAAABJRU5ErkJggg==" alt="Red dot" />

So that's what I meant, use the DATA URI scheme

查看更多
该账号已被封号
6楼-- · 2019-03-14 18:43

Use image preloading capabilities through rel="preload" attribute

<link rel="preload" href="images/mypic.jpg" as="image">

The as attribute indicates the kind of object the browser should expect. So this adds highest priorities among all images you load on page, but not change priority "JS -> Image"

<link rel="preload" href="images/mypic.jpg">

Declaration without as allow to preload images even before js load, increasing Image loading priority.

Preloading feature

查看更多
登录 后发表回答