Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of the crossorigin attribute...?

In both image and script tags.

My understanding was that you can access both scripts and images on other domains. So when does one use this attribute?

Is this when you want to restrict the ability of others to access your scripts and image?

Images:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-crossorigin

Scripts:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script

like image 995
Smurfette Avatar asked Aug 20 '13 13:08

Smurfette


People also ask

What does crossorigin attribute do?

The crossorigin attribute, valid on the <audio> , <img> , <link> , <script> , and <video> elements, provides support for CORS, defining how the element handles cross-origin requests, thereby enabling the configuration of the CORS requests for the element's fetched data.

What is the meaning of @crossorigin?

CORS stands for Cross-Origin Resource Sharing, and is a mechanism that allows resources on a web page to be requested from another domain outside their own domain. It defines a way of how a browser and server can interact to determine whether it is safe to allow the cross-origin request.

What is crossorigin in bootstrap?

Crossorigin attribute is present when a request is loaded using 'CORS' which is now a requirement of SRI checking when not loaded from the 'same-origin'. More info on crossorigin. More detail on Bootstrap CDNs implementation. Follow this answer to receive notifications.

What is the use of crossorigin anonymous?

anonymous: It has a default value. It defines a CORS request which will be sent without passing the credential information. use-credentials: A cross-origin request will be sent with credentials, cookies, and certificate.


2 Answers

The answer can be found in the specification.

For img:

The crossorigin attribute is a CORS settings attribute. Its purpose is to allow images from third-party sites that allow cross-origin access to be used with canvas.

and for script:

The crossorigin attribute is a CORS settings attribute. It controls, for scripts that are obtained from other origins, whether error information will be exposed.

like image 179
T.J. Crowder Avatar answered Sep 23 '22 21:09

T.J. Crowder


This is how we have successfully used crossorigin attribute it in a script tag:

Problem we had: We were trying to log js errors in the server using window.onerror

Almost all of the errors that we were logging had this message : Script error. and we were getting very little information about how to solve these js errors.

It turns out that the native implementation in chrome to report errors

if (securityOrigin()->canRequest(targetUrl)) {         message = errorMessage;         line = lineNumber;         sourceName = sourceURL; } else {         message = "Script error.";         sourceName = String();         line = 0; } 

will send message as Script error. if the requested static asset violates the browser's same-origin policy.

In our case we were serving the static asset from a cdn.

The way we solved it was adding the crossorigin attribute to the script tag.

P.S. Got all the info from this answer

like image 26
Omar Rayward Avatar answered Sep 21 '22 21:09

Omar Rayward