设置img src而不发出请求 [英] Set img src without issuing a request
问题描述
作为构建要复制和粘贴的代码的一部分,我们必须使用一个dom元素,并在其中添加文本/其他dom元素,最后结果就是要复制的代码.
As part of building a code to copy and paste, we had to use a dom element that we appended text / other dom elements into it, and in the end the result would be code to copy.
但是,当添加图像元素时,浏览器总是发出对图像src的请求.
However, when appending image elements, the browser always issues a request for the image src.
有什么办法解决吗?
即
var img = document.createElement('img');
img.src = 'http://dummy.com';
没有浏览器向dummy.com发出请求?
without the browser firing a request to dummy.com?
我要问的原因是我有一个模板,看起来像这样:
The reason I am asking is I have a template, that looks something like this:
<div class="container">
<div class="items-container">
<generated tags here>
</div>
</div>
其中生成的标签都包含img.
Where generated tags are with img inside.
我试图在a标签内添加图像,并获取HTML,以便我可以将其提供给用户,以便在他们的电子邮件广告系列中使用.
I was trying to append the a tags with the image inside and get the HTML so I can give it to the user to use it on their email campaigns.
但是我想它不被支持.
我最终添加了{{token}}而不是真正的html,然后使用了字符串(难看,但是有效)
I ended up adding {{token}} instead of the real html, and then used strings (ugly, but worked)
推荐答案
经过更多研究后,似乎不可能,除非缺少我缺少的浏览器API.
After some more research, it seems like it's impossible, unless there is a browser API I am missing.
任何带有src的新Image标签都会导致请求从浏览器发出,即使它只是在内存中或包装在代码块中.
Any new Image tag with src will result in a request going out from the browser, even if it's just in memory, or wrapped in a code block.
这篇关于设置img src而不发出请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!