forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen-window-dialog.js
48 lines (46 loc) · 1.7 KB
/
open-window-dialog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {includes} from './core/types/string';
import {dev} from './log';
/**
* This method wraps around window's open method. It first tries to execute
* `open` call with the provided target and if it fails, it retries the call
* with the `_top` target. This is necessary given that in some embedding
* scenarios, such as iOS' WKWebView, navigation to `_blank` and other targets
* is blocked by default.
*
* @param {!Window} win
* @param {string} url
* @param {string} target
* @param {string=} opt_features
* @return {?Window}
*/
export function openWindowDialog(win, url, target, opt_features) {
// Try first with the specified target. If we're inside the WKWebView or
// a similar environments, this method is expected to fail by default for
// all targets except `_top`.
let res;
try {
res = win.open(url, target, opt_features);
} catch (e) {
dev().error('DOM', 'Failed to open url on target: ', target, e);
}
// Then try with `_top` target.
if (!res && target != '_top' && !includes(opt_features || '', 'noopener')) {
res = win.open(url, '_top');
}
return res;
}