Why do browsers inefficiently make 2 requests here?

前端 未结 9 2182
-上瘾入骨i
-上瘾入骨i 2021-02-13 14:48

I noticed something odd regarding ajax and image loading. Suppose you have an image on the page, and ajax requests the same image - one would guess that ajax requests would hit

相关标签:
9条回答
  • 2021-02-13 15:33

    The server decides what can be cached and for how long. However, it again depends on the browser, whether or not to follow it. Most web browsers like Chrome, Firefox, Safari, Opera and IE follow it, though.

    The point that I want to make here, is that your web sever might be configured to not allow your browser to cache the content, thus, when you request the image through CSS and JS, the browser follows your server's orders and doesn't cache it and thus it requests the image twice...

    0 讨论(0)
  • 2021-02-13 15:34

    Your fiddle tries to load a resource from another domain via ajax: cross domain request

    I think I created a better example. Here is the code:

    <img src="smiley.png" alt="smiley" />
    <div id="respText"></div>
    
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script>
      $(window).load(function(){        
        $.get("smiley.png", function(){
          $("#respText").text("ajax request succeeded");
        });
      });
    </script>
    

    You can test the page here.

    According to Firebug and the chrome network panel the image is returned with the status code 200 and the image for the ajax request is coming from the cache:

    Firefox: Firebug

    Chrome: chrome network panel

    So I cannot find any unexpected behavior.

    0 讨论(0)
  • 2021-02-13 15:34

    The browser will make the 2 requests on the page, cause an image called from the css uses a get request (not ajax) too before rendering the entire page.

    The window load is similar to de attribute, and is loading before the rest of the page, then, the image from the Ajax will be requested first than the image on the div, processed during the page load.

    If u would like to load a image after the entire page is loaded, u should use the document.ready() instead

    0 讨论(0)
  • 2021-02-13 15:36

    This may be a shot in the dark, but here's what I think is happening.

    According to, http://api.jquery.com/jQuery.get/

    dataType
      Type: String
      The type of data expected from the server. 
      Default: Intelligent Guess (xml, json, script, or html).
    

    Gives you 4 possible return types. There is no datatype of image/gif being returned. Thus, the browser doesn't test it's cache for the src document as it is being delivered a a different mime type.

    0 讨论(0)
  • 2021-02-13 15:39

    I use the newest Google Chrome and it makes one request. But in your JSFIDDLE example you are loading jQuery twice. First with CSS over style attribute and second in your code over script tag. Improved: JSFIDDLE

    <div id="something" style="background-image:url('http://jsfiddle.net/img/logo-white.png');">Hello</div>
    
    <script>
        jQuery(window).load(function() {
            jQuery.get('http://jsfiddle.net/img/logo-white.png');
        });
    
        // or
    
        jQuery(function($) {
            jQuery.get('http://jsfiddle.net/img/logo-white.png');
        });
    </script>
    

    jQuery(function($) {...} is called when DOM is ready and jQuery(window).load(...); if DOM is ready and every image and other resources are loaded. To put both together nested makes no sense, see also here: window.onload vs $(document).ready()

    Sure, the image is loaded two times in Network tab of the web inspector. First through your CSS and second through your JavaScript. The second request is probably cached.

    UPDATE: But every request if cached or not is shown in this tab. See following example: http://jsfiddle.net/95mnf9rm/4/ There are 5 request with cached AJAX calls and 5 without caching. And 10 request are shown in 'Network' tab. When you use your image twice in CSS then it's only requested once. But if you explicitly make a AJAX call then the browser makes an AJAX call. As you want. And then maybe it's cached or not, but it's explicitly requested, isn't it?

    0 讨论(0)
  • 2021-02-13 15:39

    Cache control on Ajax requests have always been a blurred and buggy subject (example). The problem gets even worse with cross-domain references.

    The fiddle link you provided is from jsfiddle.net which is an alias for fiddle.jshell.net. Every code runs inside the fiddle.jshell.net domain, but your code is referencing an image from the alias and browsers will consider it a cross-domain access.

    To fix it, you could change both urls to http://fiddle.jshell.net/img/logo-white.png or just /img/logo-white.png.

    0 讨论(0)
提交回复
热议问题