You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please note that we can't get HTTP error details here. We don't know was it error 404 or 500 or something else. Just that the loading failed.
73
-
74
-
```warn
75
-
Events `onload`/`onerror` track only the loading itself.
76
-
77
-
Errors during script processing and execution are out of the scope of these events. To track script errors, one can use `window.onerror` global handler.
78
-
```
79
-
80
-
## Other resources
81
-
82
-
The `load` and `error` events also work for other resources, basically for any resource that has an external `src`.
- Most resources start loading when they are added to the document. But `<img>` is an exception. It starts loading when it gets an src `(*)`.
102
-
- For `<iframe>`, the `iframe.onload` event triggers when the iframe loading finished, both for successful load and in case of an error.
103
-
104
-
That's for historical reasons.
105
-
106
-
## Crossorigin policy
107
-
108
-
There's a rule: scripts from one site can't access contents of the other site. So, e.g. a script at `https://facebook.com` can't read the user's mailbox at `https://gmail.com`.
109
-
110
-
Or, to be more precise, one origin (domain/port/protocol triplet) can't access the content from another one. So even if we have a subdomain, or just another port, these are different origins, no access to each other.
111
-
112
-
This rule also affects resources from other domains.
113
-
114
-
If we're using a script from another domain, and there's an error in it, we can't get error details.
115
-
116
-
For example, let's take a script with a single (bad) function call:
Details may vary depending on the browser, but the idea is same: any information about the internals of a script is hidden. Exactly because it's from another domain.
72
+
`load` 和 `error` 事件也适用于其他资源。但是也存在细微的差别。
159
73
160
-
Why do we need the details?
74
+
例如:
161
75
162
-
There are many services (and we can build our own) that listen to `window.onerror`, save errors at the server and provide an interface to access and analyze them. That's great, as we can see real errors, triggered by our users. But we can't see any error information for scripts from other domains.
163
-
164
-
Similar cross-origin policy (CORS) is enforced for other types of resources as well.
165
-
166
-
**To allow cross-origin access, we need `crossorigin` attribute, plus the remote server must provide special headers.**
2.**`crossorigin="anonymous"`** -- access allowed if the server responds with the header `Access-Control-Allow-Origin` with `*` or our origin. Browser does not send authorization information and cookies to remote server.
172
-
3.**`crossorigin="use-credentials"`** -- access allowed if the server sends back the header `Access-Control-Allow-Origin` with our origin and `Access-Control-Allow-Credentials: true`. Browser sends authorization information and cookies to remote server.
173
-
174
-
```smart
175
-
You can read more about cross-origin access in the chapter <info:fetch-crossorigin>. It describes `fetch` method for network requests, but the policy is exactly the same.
176
-
177
-
Such thing as "cookies" is out of our current scope, but you can read about them in the chapter <info:cookie>.
178
-
```
179
-
180
-
In our case, we didn't have any crossorigin attribute. So the cross-origin access was prohibited. Let's add it.
181
-
182
-
We can choose between `"anonymous"` (no cookies sent, one server-side header needed) and `"use-credentials"` (sends cookies too, two server-side headers needed).
183
-
184
-
If we don't care about cookies, then `"anonymous"` is a way to go:
0 commit comments