HTMLImageElement
接口的
crossOrigin
attribute is a string which specifies the Cross-Origin Resource Sharing (
CORS
) setting to use when retrieving the image.
htmlImageElement.crossOrigin = crossOriginMode; let crossOriginMode = htmlImageElement.crossOrigin;
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
模式)。
Permitted values are:
anonymous
<img>
element have their
mode
设为
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
HTMLImageElement
将使用
cors
mode and the
包括
credentials mode; all image requests by the element will use CORS, regardless of what domain the fetch is from.
若
crossOrigin
为空字符串 (
""
),
anonymous
mode is selected.
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.
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://mdn.mozillademos.org/files/16797/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);
<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>
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%;
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'HTMLImageElement.crossOrigin' in that specification. |
未知 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
crossOrigin
|
Chrome 13 | Edge 12 | Firefox Yes | IE 9 | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android Yes | Opera Android ? | Safari iOS Yes | Samsung Internet Android Yes |
完整支持
兼容性未知
HTMLImageElement
BeforeUnloadEvent
DOMStringMap
ErrorEvent
GlobalEventHandlers
HTMLAnchorElement
HTMLAreaElement
HTMLAudioElement
HTMLBRElement
HTMLBaseElement
HTMLBaseFontElement
HTMLBodyElement
HTMLButtonElement
HTMLCanvasElement
HTMLContentElement
HTMLDListElement
HTMLDataElement
HTMLDataListElement
HTMLDialogElement
HTMLDivElement
HTMLDocument
HTMLElement
HTMLEmbedElement
HTMLFieldSetElement
HTMLFormControlsCollection
HTMLFormElement
HTMLFrameSetElement
HTMLHRElement
HTMLHeadElement
HTMLHeadingElement
HTMLHtmlElement
HTMLIFrameElement
HTMLInputElement
HTMLIsIndexElement
HTMLKeygenElement
HTMLLIElement
HTMLLabelElement
HTMLLegendElement
HTMLLinkElement
HTMLMapElement
HTMLMediaElement
HTMLMetaElement
HTMLMeterElement
HTMLModElement
HTMLOListElement
HTMLObjectElement
HTMLOptGroupElement
HTMLOptionElement
HTMLOptionsCollection
HTMLOutputElement
HTMLParagraphElement
HTMLParamElement
HTMLPictureElement
HTMLPreElement
HTMLProgressElement
HTMLQuoteElement
HTMLScriptElement
HTMLSelectElement
HTMLShadowElement
HTMLSourceElement
HTMLSpanElement
HTMLStyleElement
HTMLTableCaptionElement
HTMLTableCellElement
HTMLTableColElement
HTMLTableDataCellElement
HTMLTableElement
HTMLTableHeaderCellElement
HTMLTableRowElement
HTMLTableSectionElement
HTMLTemplateElement
HTMLTextAreaElement
HTMLTimeElement
HTMLTitleElement
HTMLTrackElement
HTMLUListElement
HTMLUnknownElement
HTMLVideoElement
HashChangeEvent
历史
ImageData
定位
MessageChannel
MessageEvent
MessagePort
Navigator
NavigatorGeolocation
NavigatorID
NavigatorLanguage
NavigatorOnLine
NavigatorPlugins
PageTransitionEvent
Plugin
PluginArray
PopStateEvent
PortCollection
PromiseRejectionEvent
RadioNodeList
Transferable
ValidityState
Window
WindowBase64
WindowEventHandlers
WindowTimers