Definition and Usage
The crossorigin attribute specifies the CORS (Cross-Origin Resource Sharing) request mode used when fetching resources linked to an element from an external origin.
- external origin
- An external origin refers to an address where at least one of the current web page's protocol (e.g.,
http://orhttps://) or domain (e.g.,example.com) is different. If a port (e.g.,example.com:8080) is specified after the domain, the port is also included as a criterion for distinguishing the origin. If the port is different, the address is considered an external origin. See Usage Notes.
- CORS (Cross-Origin Resource Sharing)
- CORS is a web standard mechanism that allows web browsers to request and access external resources that might otherwise be blocked for security reasons.
Using the crossorigin attribute allows you to safely use external resources (e.g., external images, fonts, and scripts) according to web standards by specifying the CORS request mode.
Basic Example
The following example demonstrates how to specify anonymous as the CORS request mode when fetching the external resource https://example.com/example-framework.js linked to the <script> element, ensuring that credentials are not included in the request.
<script
src="https://example.com/example-framework.js"
crossorigin="anonymous"></script>
Usage Notes
To understand the crossorigin attribute, you need to be familiar with some prerequisite concepts.
Understanding the Interaction Between Resources from External Origins and the Current Web Page
Same-Origin Policy (SOP)
Browsers implement a core security mechanism called the Same-Origin Policy (SOP). This policy restricts how documents or scripts loaded from the current origin can interact with resources from an external origin.
When resources from an external origin are linked to a web page, browsers restrict interactions with those resources for security reasons. This security rule is called the Same-Origin Policy (SOP).
Here, an external origin refers to any origin that is different from the current origin. First, let's look at the definition of same origin.
There is a URL: https://example.com/app1/index.html.
https://is called the protocol (also known as the scheme).example.comis called the host (also known as the domain)./app1/index.htmlis called the file path.
The protocol and domain must match for them to be considered the same origin. If a port is specified, the port must also match. The file path does not matter.
The following cases are considered the same origin:
http://example.com/application1/index.htmlhttp://example.com/application2/index.html
When a port (:80) is specified as in the following cases, the port must also match for them to be considered the same origin:
http://example.com:80/application1/index.htmlhttp://example.com:80/application2/index.html
The following cases are considered an external origin because different protocols are used:
http://example.com/application1https://example.com/application2
The following cases are considered an external origin because different host names are used:
http://example.com/application1https://myapplication.example.com/application2
In this way, the web distinguishes between the same origin and external origins for security purposes and restricts interactions with resources from external origins by default. The web standard mechanism that allows resources from external origins to be safely requested and used in this restricted environment is CORS, which we discussed earlier.
Cases Where the Same-Origin Policy Allows Access to Resources from an External Origin
The Same-Origin Policy does not unconditionally restrict access to resources from an external origin.
It allows limited access to resources from external origins.
A prime example is when resources are simply embedded into the current web page.
The following are some examples of resources that can be embedded from a cross-origin source.
When the origin of the current web page and the origin of the resource are different, it is referred to as cross-origin.
- External script files embedded using
<script src="…"></script> - External CSS files linked using
<link rel="stylesheet" href="…"> - Media played using
<video>and<audio> - Images displayed using
<img>and<picture> - Content embedded using
<iframe> - External resources embedded using
<object>and<embed> - Fonts applied using the CSS
@font-facerule (some browsers may require the same origin)
Cases Where the Same-Origin Policy Restricts Access to Resources from an External Origin
Simply embedding resources from an external origin does not generally restrict access.
However, when the current web page interacts with resources from an external origin, access is restricted because it may affect security.
The following are some examples of cases where the current web page interacts with resources from an external origin:
- Data communication: When the current web page communicates with resources from an external origin (e.g., fetching data through an AJAX request)
- Access to cookies and stored information: When resources from an external origin attempt to access information from the current web page (e.g., attempting to access user cookies)
- External origin resources within simply embedded resources from an external origin: When resources from an external origin exist within simply embedded resources from another external origin, access may be restricted to maintain security.
- Script execution: When attempting to execute scripts from external origin resources on the current web page, or when external origin resources attempt to call script APIs of the current web page (certain specific APIs may be allowed).
How to Interact with Resources from an External Origin Restricted by the Same-Origin Policy
Using CORS (Cross-Origin Resource Sharing), it is possible to allow interactions with resources from an external origin that are restricted by the Same-Origin Policy.
CORS (Cross-Origin Resource Sharing)
CORS (Cross-Origin Resource Sharing) is a web standard mechanism that allows interactions between the current origin, which is restricted by the Same-Origin Policy, and resources from an external origin.
This mechanism can be broadly divided into two approaches: specifying the CORS request mode using the HTML crossorigin attribute, and configuring CORS policies on the server to allow interactions with resources from an external origin.
This article covers how to specify the CORS request mode applied when fetching resources from an external origin using the crossorigin attribute.
Specifying CORS Request Modes for External Origin Resources Using the crossorigin Attribute
The crossorigin attribute value specifies the CORS request mode applied when fetching resources from an external origin.
The crossorigin attribute value specifies the CORS request mode applied when fetching resources from an external origin. This value determines how the CORS request is processed.
Values of the crossorigin Attribute
anonymous |
Specifies that credentials are not sent when making a CORS request. |
|---|---|
use-credentials |
Specifies that credentials are sent when making a CORS request (e.g., cookies, certificates, HTTP Basic authentication). |
The use of crossorigin without a value or with an empty value such as crossorigin="" is equivalent to anonymous.
Things to Keep in Mind
When the linked resource is a font file, an anonymous CORS request may be used even for a same-origin resource, depending on the browser's CORS policy.
Unlike general resources, web fonts are loaded into web pages using the CSS @font-face rule. During this process, browsers may perform CORS requests in the background. Therefore, even for same-origin web font resources, when preloading them using <link> with rel="preload", it is recommended to use crossorigin="anonymous" as well to ensure that the same CORS request mode is used as the actual font loading request.
Let's look at this case with the following example.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>rel=preload Example Source</title>
<!-- Specifies anonymous CORS mode for the web font preload request. -->
<link
rel="preload"
href="https://another-domain.com/fonts/font.woff2"
as="font"
type="font/woff2"
crossorigin="anonymous">
<!-- Links a stylesheet that contains web font styles -->
<link rel="stylesheet" href="webfont.css">
</head>
<body>
<!-- Applies the preloaded web font directly to the text -->
<h1>rel=preload Example Source</h1>
<!-- Additional content -->
</body>
</html>
In the example above, the web font is configured to be preloaded using rel="preload" on the
<link> element. Even for a same-origin web font, crossorigin="anonymous" is specified to use the same CORS request mode as the actual font loading request.
Specifications
| Specification | |
|---|---|
crossorigin
|
HTML Standard #cors-settings-attributes |