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:
I think the reason is because
window.open
does not open local file (eg, testing.html). Hence I have installedhttp-server
to host my files on localhost.http-server
http-server