Safeguard
Developer Tools•

What does the crossorigin attribute do on a script tag?

The crossorigin attribute makes the browser fetch a script with CORS. SRI needs it for cross-origin files, and it also unlocks full error details.

Safeguard Security Team
3 min read

The crossorigin attribute tells the browser to request a file using CORS. Subresource Integrity needs it for cross-origin scripts, because a request without CORS will fail when integrity is present. Use crossorigin="anonymous" for CDN files so credentials are not sent, and confirm the server returns CORS headers.

Values

ValueBehavior
anonymousUses CORS. Credentials such as cookies are sent only to the same origin
use-credentialsUses CORS and always includes credentials
Empty attributeSame as anonymous
Invalid keywordTreated as anonymous
Attribute absentCORS is not used at all

These come from MDN's crossorigin attribute page. For scripts, a missing attribute also limits what error information reaches window.onerror, and MDN's script page says to use crossorigin to allow error logging for sites that serve static files from a separate domain.

Why SRI cares

MDN's SRI guide says cross-origin requests that use integrity must use CORS. Without the crossorigin attribute the request goes out without CORS, and the browser will always fail it when an integrity value is present. The reason given is that allowing it would let an attacker use hash matching to infer the contents of a resource.

So the pair is always written together:

<script
  src="https://cdn.example.com/lib.js"
  integrity="sha384-REPLACE_WITH_REAL_HASH"
  crossorigin="anonymous"></script>

The hash is a placeholder. Generate yours from the exact file.

What the server must send

The remote server must respond with an Access-Control-Allow-Origin header that permits your page. Public CDNs commonly allow any origin. A vendor's own server may not, in which case hashed loading will fail and you will need to self-host or ask them to enable CORS.

Troubleshooting a blocked script

  1. Open the browser console. A mismatch and a CORS failure produce different messages.
  2. Check the network tab for the response headers on the script request.
  3. Confirm the crossorigin attribute is present on the tag.
  4. Recompute the hash from the exact bytes the server returns, since a CDN may minify or change encoding.
  5. Make sure no other layer rewrites the file, such as an optimizing proxy.

Anonymous or use-credentials

Use anonymous for public libraries. Choose use-credentials only when the resource requires your cookies, which is rare for scripts. It forces the server to answer with stricter CORS headers, so it is harder to get right.

Module scripts

MDN notes that, unlike classic scripts, module scripts require CORS for cross-origin fetching. See does SRI work with ES modules and import maps.

Safeguard's dependency scanning covers packages in your own build, with a free scan at gold.safeguard.sh/scan.

Quick check

Can I put integrity on a script without crossorigin?

For a cross-origin file, no. The request will fail. Add crossorigin and make sure the server allows CORS.

Does crossorigin send my cookies to the CDN?

With anonymous, credentials are sent only to the same origin, so a CDN on another origin does not receive them.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.