English 中文(简体)
卸载后:如果图像出现在浏览器中,检查
原标题:Post-loading : check if an image is in the browser cache

Short version question : Is there navigator.mozIsLocallyAvailable equivalent function that works on all browsers, or an alternative?

<>0>

Hi, Here is my situation : I want to implement an HtmlHelper extension for asp.net MVC that handle image post-loading easily (using jQuery).

So i render the page with empty image sources with the source specified in the "alt" attribute. I insert image sources after the "window.onload" event, and it works great.

我这样做了:

$(window).bind( load , function() {
    var plImages = $(".postLoad");
    plImages.each(function() {
        $(this).attr("src", $(this).attr("alt"));
    });
});

www.un.org/Depts/DGACM/index_spanish.htm 问题在于:。 在第一次装货之后,载后图像被加热。 但是,如果该网页需要10秒钟才能装载,则在这10秒后将展示有卡的后载图像。

因此,如果图像能够立即显示,就想在“文件读物”活动中具体说明图像来源。

我发现这一职能:当地人 如果有图像在海滩上,可以检查。 这里是我用 j子做的:

//specify cached image sources on dom ready
$(document).ready(function() {
    var plImages = $(".postLoad");
    plImages.each(function() {
        var source = $(this).attr("alt")
        var disponible = navigator.mozIsLocallyAvailable(source, true);
        if (disponible)
            $(this).attr("src", source);
    });
});

//specify uncached image sources after page loading
$(window).bind( load , function() {
        var plImages = $(".postLoad");
        plImages.each(function() {
        if ($(this).attr("src") == "")
            $(this).attr("src", $(this).attr("alt"));
    });
});

该公司在“DOM”公司工作,但在任何其他公司工作。 我尝试了导航器。 当地雇员 现有:相同结果。

是否有任何替代办法?

最佳回答

在进行了一些回击之后,我找到了解决办法:

The idea is to log the cached images, binding a log function on the images load event. I first thought to store sources in a cookie, but it s not reliable if the cache is cleared without the cookie. Moreover, it adds one more cookie to HTTP requests...

随后,我会见了:window. localStorage(,details

The localStorage attribute provides persistent storage areas for domains

事实上,我想要的是: 这一特性在超文本5中是标准化的,它已经在几乎所有最近的浏览器(FF、OM、Sissa、IE8、Mack)上运行。

这里是该法典(没有处理窗户、当地供应商不兼容的浏览器):

var storage = window.localStorage;
if (!storage.cachedElements) {
    storage.cachedElements = "";
}

function logCache(source) {
    if (storage.cachedElements.indexOf(source, 0) < 0) {
        if (storage.cachedElements != "") 
            storage.cachedElements += ";";
        storage.cachedElements += source;
    }
}

function cached(source) {
    return (storage.cachedElements.indexOf(source, 0) >= 0);
}

var plImages;

//On DOM Ready
$(document).ready(function() {
    plImages = $(".postLoad");

    //log cached images
    plImages.bind( load , function() {
        logCache($(this).attr("src"));
    });

    //display cached images
    plImages.each(function() {
        var source = $(this).attr("alt")
        if (cached(source))
            $(this).attr("src", source);
    });
});

//After page loading
$(window).bind( load , function() {
    //display uncached images
    plImages.each(function() {
        if ($(this).attr("src") == "")
            $(this).attr("src", $(this).attr("alt"));
    });
});
问题回答

检查图像是否已经贴出最高效、简单和得到广泛支持的方法是:

  1. Create an image object
  2. Set the src property to the desired url
  3. Check the completed attribute immediately to see if the image is already cached
  4. Set the src attribute back to "" (empty string), so that the image is not unnecessarily loaded (unless of coarse you want to load it at this time)

和......

function isCached(src) {
    const img = new Image();
    img.src = src;
    const complete = img.complete;
    img.src = "";
    return complete;
}

如果是的话,可以这样执行。

const lazyImages = document.querySelectorAll(".postLoad");
for (const img of lazyImages) {
    if ((!img.src || !isCached(img.src)) && img.getAttribute("alt")) {
        img.src = img.getAttribute("alt");
    }
}

尽管如此,我建议不要为此目的使用斜体,而是应该使用<条码>数据-src。

如果播放画面有线索,就将几乎立即回去。 然后使用规定时间来确定其是否准备就绪并取消请求,以便你日后重新排列。

<>Update:

var lqueue = [];
$(function() {
  var t,ac=0;
  (t = $("img")).each(
    function(i,e)
    {
      var rq = $.ajax(
      {
        cache: true,
        type: "GET",
        async:true,
        url:e.alt,
        success: function() { var rq3=rq; if (rq3.readyState==4) { e.src=e.alt; } },
        error: function() { e.src=e.alt; }
      });

      setTimeout(function()
      {
        var k=i,e2=e,r2=rq;
        if (r2.readyState != 4)
        {
          r2.abort();
          lqueue.push(e2);
        }
        if (t.length==(++ac)) loadRequeue();
      }, 0);
    }
  );
});

function loadRequeue()
{
  for(var j = 0; j < lqueue.length; j++) lqueue[j].src=lqueue[j].alt;
}

比如,其他人可能会遇到同样的问题。 这里提供的一些解决办法(即在当地浏览器数据储存中储存海滩)可能因两个原因中断。 首先,如果图像的切片过期,其次,如果用户清除海滩的话。 另一种做法是将图像来源确定给一名地方持有人。 然后将来源改为图像途径/名称。 这样,浏览器就有责任检查自己的海滩。 应当与多数浏览者合作,而不管他们是怎样的。

任何人如果试图用React I来解决这一问题,就使用complete图像财产,以这种方式解决:

import React, { useState, useEffect, useRef } from  react 

const Component= () => {
  const [isLoadedImage, setLoadedImage] = useState(false)
  const imageRef = useRef(null)

  useEffect(() => {
    const imgEl = imageRef.current
    if (imgEl && imgEl.complete && !isLoadedImage) setLoadedImage(true)
  })

  return (
    <img
      onLoad={() => (!isLoadedImage ? setLoadedImage(true) : null)}
      ref={imageRef}
    />
  )
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签