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: 4-frames-and-windows/03-cross-window-communication/article.md
+56-56Lines changed: 56 additions & 56 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,126 +1,123 @@
1
-
# Cross-window communication
1
+
# ウィンドウ間のやり取り
2
2
3
-
The "Same Origin" (same site) policy limits access of windows and frame to each other.
3
+
"同一オリジン" (同一サイト) ポリシーは、ウィンドウとフレームのアクセスを互いに制限します。
4
4
5
-
The idea is that if we have two windows open: one from `john-smith.com`, and another one is `gmail.com`, then we wouldn't want a script from `john-smith.com`to read our mail.
If we have a reference to another window (a popup or iframe), and that window comes from the same origin, then we can do everything with it.
26
+
"同一オリジン" ポリシーは次のようになります:
27
27
28
-
If it comes from another origin, then we can only change its location. Please note: not *read* the location, but *modify* it, redirect it to another place. That's safe, because the URL may contain sensitive parameters, so reading it from another origin is prohibited, but changing is not.
If windows share the same second-level domain, for instance `john.site.com`, `peter.site.com` and `site.com`, we can use JavaScript to assign to `document.domain` their common second-level domain `site.com`. Then these windows are treated as having the same origin.
37
-
38
-
In other words, all such documents (including the one from `site.com`) should have the code:
When we access an embedded window, the browser checks if the iframe has the same origin. If that's not so then the access is denied (with exclusions noted above).
That's actually a well-known pitfall for novice developers. We shouldn't work with the document immediately, because that's the *wrong document*. If we set any event handlers on it, they will be ignored.
...But the `onload`event triggers when the whole iframe with all resources is loaded. What if we want to act sooner, on `DOMContentLoaded`of the embedded document?
That's not possible if the iframe comes from another origin. But for the same origin we can try to catch the moment when a new document appears, and then setup necessary handlers, like this:
@@ -209,7 +206,7 @@ if (window == top) { // current window == window.top?
209
206
210
207
## The sandbox attribute
211
208
212
-
The `sandbox` attribute allows to forbid certain actions inside an `<iframe>`, to run an untrusted code. It "sandboxes" the iframe by treating it as coming from another origin and/or applying other limitations.
209
+
The `sandbox` attribute allows for the exclusion of certain actions inside an `<iframe>` in order to prevent it executing untrusted code. It "sandboxes" the iframe by treating it as coming from another origin and/or applying other limitations.
213
210
214
211
By default, for `<iframe sandbox src="...">` the "default set" of restrictions is applied to the iframe. But we can provide a space-separated list of "excluded" limitations as a value of the attribute, like this: `<iframe sandbox="allow-forms allow-popups">`. The listed limitations are not applied.
215
212
@@ -249,7 +246,9 @@ The purpose of the `"sandbox"` attribute is only to *add more* restrictions. It
249
246
250
247
The `postMessage` interface allows windows to talk to each other no matter which origin they are from.
251
248
252
-
It has two parts.
249
+
So, it's a way around the "Same Origin" policy. It allows a window from `john-smith.com` to talk to `gmail.com` and exchange information, but only if they both agree and call corresponding Javascript functions. That makes it safe for users.
250
+
251
+
The interface has two parts.
253
252
254
253
### postMessage
255
254
@@ -351,6 +350,7 @@ Otherwise, only possible actions are:
351
350
- Change the location of another window (write-only access).
352
351
- Post a message to it.
353
352
353
+
354
354
Exclusions are:
355
355
- Windows that share the same second-level domain: `a.site.com` and `b.site.com`. Then setting `document.domain='site.com'` in both of them puts them into the "same origin" state.
356
356
- If an iframe has a `sandbox` attribute, it is forcefully put into the "different origin" state, unless the `allow-same-origin` is specified in the attribute value. That can be used to run untrusted code in iframes from the same site.
@@ -364,4 +364,4 @@ The `postMessage` interface allows two windows to talk with security checks:
364
364
-`source` -- the reference to the sender window.
365
365
-`data` -- the data, any object in everywhere except IE that supports only strings.
366
366
367
-
We should use `addEventListener` to set the handler for this event inside the target window.
367
+
We should use `addEventListener` to set the handler for this event inside the target window.
0 commit comments