问题
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?
(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.
In my WKWebView I have done the following:
- Implemented
WKUIDelegate
- Set
_webView.UIDelegate = self;
- Set both the preferences:
wkWebViewConfig.preferences.javaScriptCanOpenWindowsAutomatically = YES; wkWebViewConfig.preferences.javaScriptEnabled = YES;
- Create method below
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
if (!navigationAction.targetFrame.isMainFrame) {
[webView loadRequest:navigationAction.request];
}
return nil;
}
My sample html source as follow:
<!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>
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/33198751/4311268
https://stackoverflow.com/a/39073499/4311268
https://stackoverflow.com/a/25853806/4311268
回答1:
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 to my folder where all the html file placed
- run
http-server
- Run the last IP address:8080 return from
http-server
- You will see all your .html files there in localhost
- Replace your .html file with localhost link
来源:https://stackoverflow.com/questions/60851538/wkwebview-does-not-trigger-method-createwebviewwithconfiguration