[Javascript] Create an Image with JavaScript Using Fetch and URL.createObjectURL
Most developers are familiar with using img tags and assigning the src inside of HTML. It is also possible to only use JavaScript to fetch the image from another site, create a local url, and assign that to an img tag that you create. This lesson walks you through the process of fetching an image from a placeholder site and displaying it using only JavaScript.
;(async function() { const response = await fetch(`https://placekitten.com/320/240`) const blob = await response.blob() const url = URL.createObjectURL(blob) const image = new Image() image.src = url document.getElementById("app").appendChild(image) })()
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步