问题描述
我正在尝试在 HTML 源中运行 Javascript.里面的代码假设用我给出的另一个 HTML 源打开一个新窗口.我这里有什么错误?
I'm trying to run Javascript within in a HTML Source. The code inside is suppose to open a new window with another HTML Source I given. What's my mistake here?
(我的目标是证明WKWebView有能力打开嵌套弹出窗口)意思是,Webview打开了一个PopUpWindow A,然后PopUpWindow A会window.open()
PopUpWindow B,然后PopUpWindow B 将window.open()
PopUpWindow C.
(My goal is to prove the WKWebView has ability in opening nested popup window) Meaning that, Webview opened a PopUpWindow A, then PopUpWindow A will window.open()
PopUpWindow B, then PopUpWindow B will window.open()
PopUpWindow C.
在我的 WKWebView 中,我完成了以下操作:
In my WKWebView I have done the following:
- 已实现
WKUIDelegate
- 设置
_webView.UIDelegate = self;
- 设置两个首选项:
wkWebViewConfig.preferences.javaScriptCanOpenWindowsAutomatically = YES;wkWebViewConfig.preferences.javaScriptEnabled = YES;
- 在下面创建方法
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
if (!navigationAction.targetFrame.isMainFrame) {
[webView loadRequest:navigationAction.request];
}
return nil;
}
我的示例 html 源代码如下:
<!DOCTYPE html>
<html>
<head>
<script language="javascript">
function init()
{
setTimeout( () => {
window.open("testing2.html","mywindow","toolbar=no,menubar=no,location=no,directories=no,width=910,height=750");
}, 3000)
document.redirectForm.target="mywindow";
document.redirectForm.submit();
}
</script>
</head>
<body>
Going to testing2
<form>
<input type="button" onclick="init()" value="Click" />
</form>
<script type="text/javascript">
init();
</script>
</body>
</html>
我尝试用 https://www.google.com
替换 "testing2.html"
,它确实显示了 Google 网站.但同样,我的目标是确保我的 WKWebView 由于某些客户端 API 的某些架构设计而能够打开嵌套弹出窗口
I tried replacing "testing2.html"
with https://www.google.com
, it does show up Google website. But again, my goal here is to ensure my WKWebView is able to open Nested PopUp Window due to some architecture design of some client API
我读过的一些类似问题和答案:
Some similar ques and ans I've read:
https://stackoverflow.com/a/39073499/4311268
https://stackoverflow.com/a/25853806/4311268
推荐答案
我认为是因为 window.open
没有打开本地文件(例如 testing.html).因此,我安装了 http-server
来在本地主机上托管我的文件.
I think the reason is because window.open
does not open local file (eg, testing.html). Hence I have installed http-server
to host my files on localhost.
- npm i http-server -g
- cd 到我放置所有 html 文件的文件夹
- 运行
http-server
- 运行最后一个IP地址:8080从
http-server
返回 - 您将在 localhost 中看到所有 .html 文件
- 将您的 .html 文件替换为 localhost 链接
这篇关于WKWebView 不触发方法 createWebViewWithConfiguration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!