English 中文(简体)
• 如何在贾瓦特树立形象
原标题:How do you cache an image in Javascript

我的朋友和我正在一个网站上工作,我们希望在网站上收集某些图像,以便今后更快地展示这些图像。 我有两个主要问题:

  1. How do you cache an image?
  2. How do you use an image once it has been cached? (and just to verify, if an image is cached on page A, it is possible to call it from the cache to use it on page B, right?)

也可在<>上设定。 图像的焦土版将到期?

如果列入一个例子和(或)与另一个页的链接,则将不胜感激。

我们利用 raw本或 j本进行罚款。

最佳回答

一旦图像以任何方式装入浏览器,就将装在浏览器中,并将在下一年使用时装上更快的垫子,只要图像在浏览器切身之前使用,这种使用是否在目前的网页上或在其他网页上。

因此,为了预示图像,你必须做的是把图像装入浏览器。 如果你想预示一幅图像,那很可能最好用javascript做,因为通常从javascript处打上页数。 你可以这样做:

function preloadImages(array) {
    if (!preloadImages.list) {
        preloadImages.list = [];
    }
    var list = preloadImages.list;
    for (var i = 0; i < array.length; i++) {
        var img = new Image();
        img.onload = function() {
            var index = list.indexOf(this);
            if (index !== -1) {
                // remove image from the array once it s loaded
                // for memory consumption reasons
                list.splice(index, 1);
            }
        }
        list.push(img);
        img.src = array[i];
    }
}

preloadImages(["url1.jpg", "url2.jpg", "url3.jpg"]);

这一职能可以像你所希望的多次一样被称作,而且每次都会给预谋增添更多的形象。

一旦图像通过javascript上载,浏览器就会把图像放在其藏身处,而且你可以仅提及其他地方(在你的网页上)的普通URLs,浏览器会把URL从它的切身上而不是从网络上排出来。

最终随着时间的推移,浏览器海滩可以填充和存放过去曾经使用过的旧东西。 因此,这些图像最终会流出海滩,但应当暂时留在那里(取决于海滩的大小以及其他浏览量如何)。 每当这些图像实际上被再次装上或用在网页上时,它都会自动恢复其在浏览器的海滩上的地位,因此他们不太可能从海滩上流出。

browser cache是交叉页,用于装入浏览器的任何页面。 因此,你可以在贵址的某个地方预设,而浏览器的海滩则在你网站上所有其他网页上工作。


如上所示,这些图像的装载时间是同步的,不会阻挡你页的装载或显示。 但是,如果你的网页有自己的图像,这些预想的图像可以竞争带宽或与你网页上展示的图像连接。 通常,这只是一个值得注意的问题,但就缓慢的关系而言,这一引诱可能会减缓主页的负荷。 如果是最后装上前载图像的大韩民国,那么你就可以使用一种功能版本,在所有其他页面资源都已装满之后,才能开始上载。

function preloadImages(array, waitForOtherResources, timeout) {
    var loaded = false, list = preloadImages.list, imgs = array.slice(0), t = timeout || 15*1000, timer;
    if (!preloadImages.list) {
        preloadImages.list = [];
    }
    if (!waitForOtherResources || document.readyState ===  complete ) {
        loadNow();
    } else {
        window.addEventListener("load", function() {
            clearTimeout(timer);
            loadNow();
        });
        // in case window.addEventListener doesn t get called (sometimes some resource gets stuck)
        // then preload the images anyway after some timeout time
        timer = setTimeout(loadNow, t);
    }

    function loadNow() {
        if (!loaded) {
            loaded = true;
            for (var i = 0; i < imgs.length; i++) {
                var img = new Image();
                img.onload = img.onerror = img.onabort = function() {
                    var index = list.indexOf(this);
                    if (index !== -1) {
                        // remove image from the array once it s loaded
                        // for memory consumption reasons
                        list.splice(index, 1);
                    }
                }
                list.push(img);
                img.src = imgs[i];
            }
        }
    }
}

preloadImages(["url1.jpg", "url2.jpg", "url3.jpg"], true);
preloadImages(["url99.jpg", "url98.jpg"], true);
问题回答

@Pointy 说,你没有用javascript images,浏览器就是这样。 因此,这或许是你要求的东西,也可能不是......,但你可以用javascript顶点图像。 通过把你想要把所有图像放在一个阵列中,把该阵列中的所有图像都带入隐蔽的图像,你有效地将图像(或切)上载。

var images = [
 /path/to/image1.png ,
 /path/to/image2.png 
];

$(images).each(function() {
var image = $( <img /> ).attr( src , this);
});

增加答复的完整性:以超文本载

<link rel="preload" href="bg-image-wide.png" as="image">

其他载荷特征是存在的,但无一可用于下列用途:<link rel=“preload”>:

  • <link rel="prefetch"> has been supported in browsers for a long time, but it is intended for prefetching resources that will be used in the next navigation/page load (e.g. when you go to the next page). This is fine, but isn t useful for the current page! In addition, browsers will give prefetch resources a lower priority than preload ones — the current page is more important than the next. See Link prefetching FAQ for more details.
  • <link rel="prerender"> renders a specified webpage in the background, speeding up its load if the user navigates to it. Because of the potential to waste users bandwidth, Chrome treats prerender as a NoState prefetch instead.
  • <link rel="subresource"> was supported in Chrome a while ago, and was intended to tackle the same issue as preload, but it had a problem: there was no way to work out a priority for the items (as didn t exist back then), so they all got fetched with fairly low priority.

那里有一些以文字为基础的资源载荷,但它们对浏览器的排他性排列次序没有权力,并且遇到许多同样的绩效问题。

Source: https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content

如今,有一个新的

  1. Include the JavaScript Lazysizes file: lazysizes.js
  2. Add the file to the html file you want to use in: <script src="lazysizes.min.js" async></script>
  3. Add the lazyload class to your image: <img data-src="images/flower3.png" class="lazyload" alt="">

你们可以看一看:

Pre-loading your images
Setting a cache time in an .htaccess file
File size of images and base64 encoding them.

Preloading: http://perishablepress.com/3-ways-preload-images-css-javascript-ajax/

Caching: http://www.askapache.com/htaccess/speed-up-sites-with-htaccess-caching.html

基础64编码有两种不同的想法,有些人说,http://www.un.org/ga/wth,而另一些人则说,“接收”装载量更好。 飞机停下。

尽管你的问题说“使用javascript”,但你可以使用prefetch称号链接标签,以顶上任何资产。 截至本著作(Aug 10,2016年)为止,它在Safat得到支持,但在其他地方则得到支持:

<link rel=“prefetch”href=“(url)”>

More info on support here: http://caniuse.com/#search=prefetch

Note that IE 9,10 aren t listed in the caniuse matrix because Microsoft has discontinued support for them.

因此,如果你真心不敢使用javascript,那么你也可以用 j子在你网页上积极添加这些内容。

I use a similar technique to lazyload images, but can t help but notice that Javascript doesn t access the browser cache on first loading.

我的例子:

我的主页上有一个轮值的旗帜,有4张画像,头等2秒,比 j带下下一个形象,等待2秒。

这些图像具有独特的旋律,每当我对图像进行改动时,就会发生变化,因此,这些图像的头盔将迎击浏览器,每年在浏览器中ache。

max-age: 31536000, public

现在,当我打开了Dawatools,确保了可处置的海滩选择不积极,首次(在清理海滩之后)装上了该页,所有图像都有点光,具有200个身份。 在整个横幅标语中的所有图像之后,网络要求停止,并使用有线图的图像。

现在,当我定期复读或去一页,然后点击时,似乎忽略了藏匿的图像。 我预计会在网络刺tab Chrome中看到“从软盘切”的灰色信息。 相反,我看到每二秒有绿色地位圈子而不是灰色的传票,我看到数据被转移,因此,我得到的印象是,海滩根本无法从javascript处获得。 每当页装上时,它就只能打上图像。

So each request to the homepage triggers 4 requests regardless of the caching policy of the image.

考虑到上述内容以及新的http2标准,大多数网络服务器和浏览器现在都得到支持,我认为,由于http2将几乎同时装载所有图像,因此最好停止使用电荷载。

If this is a bug in Chrome Devtools it really surprises my nobody noticed this yet. ;)

If this is true, using lazyloading only increases bandwith usage.

如果我错的话,请更正:

I have a similar answer for asynchronous preloading images via JS. Loading them dynamically is the same as loading them normally. they will cache.

至于海滩,你可以控制浏览器,但可以通过服务器安装。 如果你需要装上真正的新鲜资源,你可使用cache Buster Technologies 强迫装载新资源。





相关问题
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.

热门标签