HTMLImageElement.crossOrigin - Web APIs 编辑

The HTMLImageElement interface's crossOrigin attribute is a string which specifies the Cross-Origin Resource Sharing (CORS) setting to use when retrieving the image.

Syntax

htmlImageElement.crossOrigin = crossOriginMode;
let crossOriginMode = htmlImageElement.crossOrigin;

Value

A DOMString of a keyword specifying the CORS mode to use when fetching the image resource. If you don't specify crossOrigin, the image is fetched without CORS (the fetch no-cors mode).

Permitted values are:

anonymous
Requests by the <img> element have their mode set to cors and their credentials mode set to same-origin. This means that CORS is enabled and credentials are sent if the image is fetched from the same origin from which the document was loaded.
use-credentials
Requests by the HTMLImageElement will use the cors mode and the include credentials mode; all image requests by the element will use CORS, regardless of what domain the fetch is from.

If crossOrigin is an empty string (""), the anonymous mode is selected.

Example

In this example, a new <img> element is created and added to the document, loading the image with the Anonymous state; the image will be loaded using CORS and credentials will be used for all cross-origin loads.

JavaScript

The code below demonstrates setting the crossOrigin property on an <img> element to configure CORS access for the fetch of a newly-created image.

const imageUrl = "https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/clock-demo-400px.png";
const container = document.querySelector(".container");

function loadImage(url) {
  const image = new Image(200, 200);
  image.addEventListener("load",
    () => container.prepend(image)
  );

  image.addEventListener("error", () => {
    const errMsg = document.createElement("output");
    errMsg.value = `Error loading image at ${url}`;
    container.append(errMsg);
  });

  image.crossOrigin = "anonymous";
  image.alt = "";
  image.src = url;
}

loadImage(imageUrl);

HTML

<div class="container">
  <p>Here's a paragraph. It's a very interesting paragraph. You
  are captivated by this paragraph. Keep reading this paragraph.
  Okay, now you can stop reading this paragraph. Thanks for
  reading me.</p>
</div>

CSS

body {
  font: 1.125rem/1.5, Helvetica, sans-serif;
}

.container {
  display: flow-root;
  width: 37.5em;
  border: 1px solid #d2d2d2;
}

img {
  float: left;
  padding-right: 1.5em;
}

output {
  background: rgba(100, 100, 100, 0.1);
  font-family: Courier, monospace;
  width: 95%;
}

Result

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of 'HTMLImageElement.crossOrigin' in that specification.
Unknown

Browser compatibility

BCD tables only load in the browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:119 次

字数:5247

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文