js ajax 加载图片 img的简单方法

例如网页有很多img图片,这样整个网页加载起来会变得很慢,这里可以先通过xmlhttprequest来请求,在设置到img的src中

var xhr = new XMLHttpRequest();
            xhr.open("GET", url, true);
            

            xhr.responseType = "blob";
            xhr.onload = function()
            {
                if (this.readyState != 4)
                    return;

                var reader = new FileReader();
                reader.onload = function()
                {
                    $(imgObj).attr("src",this.result);
                    
                };
                reader.readAsDataURL(this.response);
            }
            xhr.send();


你可能感兴趣的:(html)