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
Copy file name to clipboardExpand all lines: 2-ui/5-loading/03-onload-onerror/article.md
+11-11
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Resource loading: onload and onerror
2
2
3
-
The browser allows to track the loading of external resources -- scripts, iframes, pictures and so on.
3
+
The browser allows us to track the loading of external resources -- scripts, iframes, pictures and so on.
4
4
5
5
There are two events for it:
6
6
@@ -49,11 +49,11 @@ script.onload = function() {
49
49
50
50
So in `onload` we can use script variables, run functions etc.
51
51
52
-
...And what if the loading failed? For instance, there's no such script (error 404) or the server or the server is down (unavailable).
52
+
...And what if the loading failed? For instance, there's no such script (error 404) or the server is down (unavailable).
53
53
54
54
### script.onerror
55
55
56
-
Errors that occur during the loading of the script can be tracked on`error` event.
56
+
Errors that occur during the loading of the script can be tracked in an`error` event.
57
57
58
58
For instance, let's request a script that doesn't exist:
59
59
@@ -69,12 +69,12 @@ script.onerror = function() {
69
69
*/!*
70
70
```
71
71
72
-
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.
72
+
Please note that we can't get HTTP error details here. We don't know if it was an error 404 or 500 or something else. Just that the loading failed.
73
73
74
74
```warn
75
75
Events `onload`/`onerror` track only the loading itself.
76
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.
77
+
Errors during script processing and execution are out of scope for these events. To track script errors, one can use `window.onerror` global handler.
78
78
```
79
79
80
80
## Other resources
@@ -98,7 +98,7 @@ img.onerror = function() {
98
98
99
99
There are some notes though:
100
100
101
-
- Most resources start loading when they are added to the document. But `<img>` is an exception. It starts loading when it gets an src `(*)`.
101
+
- Most resources start loading when they are added to the document. But `<img>` is an exception. It starts loading when it gets a src `(*)`.
102
102
- For `<iframe>`, the `iframe.onload` event triggers when the iframe loading finished, both for successful load and in case of an error.
103
103
104
104
That's for historical reasons.
@@ -155,15 +155,15 @@ Script error.
155
155
, 0:0
156
156
```
157
157
158
-
Details may vary depending on the browser, but the idea is same: any information about the internals of a script, including error stack traces, is hidden. Exactly because it's from another domain.
158
+
Details may vary depending on the browser, but the idea is the same: any information about the internals of a script, including error stack traces, is hidden. Exactly because it's from another domain.
159
159
160
160
Why do we need error details?
161
161
162
162
There are many services (and we can build our own) that listen for global errors using `window.onerror`, save errors and provide an interface to access and analyze them. That's great, as we can see real errors, triggered by our users. But if a script comes from another origin, then there's no much information about errors in it, as we've just seen.
163
163
164
164
Similar cross-origin policy (CORS) is enforced for other types of resources as well.
165
165
166
-
**To allow cross-origin access, the `<script>` tag needs to have `crossorigin` attribute, plus the remote server must provide special headers.**
166
+
**To allow cross-origin access, the `<script>` tag needs to have the `crossorigin` attribute, plus the remote server must provide special headers.**
167
167
168
168
There are three levels of cross-origin access:
169
169
@@ -172,7 +172,7 @@ There are three levels of cross-origin access:
172
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
173
174
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.
175
+
You can read more about cross-origin access in the chapter <info:fetch-crossorigin>. It describes the `fetch` method for network requests, but the policy is exactly the same.
176
176
177
177
Such thing as "cookies" is out of our current scope, but you can read about them in the chapter <info:cookie>.
178
178
```
@@ -181,7 +181,7 @@ In our case, we didn't have any crossorigin attribute. So the cross-origin acces
181
181
182
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
183
184
-
If we don't care about cookies, then `"anonymous"` is a way to go:
184
+
If we don't care about cookies, then `"anonymous"` is the way to go:
0 commit comments