[英]Add crossorigin="anonymous" to all img tags before the images are loaded
I'm having problem with my canvas throwing errors because of it trying to load cached images described here: https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image我的画布抛出错误时遇到问题,因为它尝试加载此处描述的缓存图像: https : //developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image
To solve this I want to add crossorigin="anonymous"
to all the images on my site, but how can I do that before the images are loaded, so that they are loaded with the proper headers that won't crash my canvas?为了解决这个问题,我想将
crossorigin="anonymous"
添加到我网站上的所有图像,但是在加载图像之前我怎么做,以便它们加载正确的标题,不会使我的画布崩溃?
$('img').each(function() { var $img = $(this); $img.attr('crossorigin',anonymous); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <img width="50%" src="https://ychef.files.bbci.co.uk/1600x900/p08ysrxd.webp">
The way you are doing should work (after fixing the syntax errors), you have to handle the setting of the crossOrigin
attribute before the next microtask checkpoint , meaning you must do it synchronously and not in an async script or in an event handler.您正在做的方式应该有效(在修复语法错误之后),您必须在下一个microtask checkpoint之前处理
crossOrigin
属性的设置,这意味着您必须同步进行,而不是在异步脚本或事件处理程序中进行。
However, browsers didn't always follow this rule and in some, the loading of cached images could be done synchronously.然而,浏览器并不总是遵循这个规则,在某些情况下,缓存图像的加载可以同步完成。 So to avoid falling in such case, you could simply set the
src
again to the same value, in order to force the browser to load the resource using the proper headers.因此,为了避免出现这种情况,您可以简单地再次将
src
设置为相同的值,以强制浏览器使用正确的标头加载资源。
$('img').each(function() { var $img = $(this); $img.attr({ crossorigin: "anonymous", src: this.src }); }); // test we loaded it with the proper headers $(window).on("load", () => { const canvas = document.createElement("canvas"); canvas.getContext("2d").drawImage($('img')[0], 0, 0); console.log('cross-origin enabled', !!canvas.toDataURL()); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <img width="50%" src="https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png">
But in your position I'd double check why I need to do it at this time, the proper fix is obviously to set the attribute from the beginning:但是在您的位置上,我会仔细检查为什么此时需要这样做,正确的解决方法显然是从一开始就设置属性:
// test we loaded it with the proper headers window.onload = () => { const canvas = document.createElement("canvas"); canvas.getContext("2d").drawImage( document.querySelector('img'), 0, 0); console.log('cross-origin enabled', !!canvas.toDataURL()); };
<img crossorigin="anonymous" width="50%" src="https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png">
Note however that the image in your question is being served by Amazon S3 with a Vary: Origin
header only when requested as cross-origin, which will make Chrome still use the cached version, even if the crossOrigin
attribute is set, see this Q/A for more info .但是请注意,您问题中的图像由 Amazon S3 提供,仅在作为跨域请求时才使用
Vary: Origin
标头,这将使 Chrome 仍使用缓存版本,即使设置了crossOrigin
属性,请参阅此 Q/ A 了解更多信息。
The only way around in that case is to always set the crossOrigin
attribute.在这种情况下,唯一的方法是始终设置
crossOrigin
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.