1

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:

  1. Implemented WKUIDelegate
  2. Set _webView.UIDelegate = self;
  3. Set both the preferences: wkWebViewConfig.preferences.javaScriptCanOpenWindowsAutomatically = YES; wkWebViewConfig.preferences.javaScriptEnabled = YES;
  4. 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:

  1. https://stackoverflow.com/a/33198751/4311268

  2. https://stackoverflow.com/a/39073499/4311268

  3. https://stackoverflow.com/a/25853806/4311268

TommyLeong
  • 832
  • 1
  • 10
  • 30

1 Answers1

0

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.

  1. npm i http-server -g
  2. cd to my folder where all the html file placed
  3. run http-server
  4. Run the last IP address:8080 return from http-server
  5. You will see all your .html files there in localhost
  6. Replace your .html file with localhost link
TommyLeong
  • 832
  • 1
  • 10
  • 30