Nw.js: when target="_blank" webview can not open url

Created on 13 Apr 2017  路  8Comments  路  Source: nwjs/nw.js

  1. version锛歯wjs-sdk-v0.21.5-win-x64
  2. webview page
<div class="webview">
    <webview id="foo" src="http://xxxx/index.html" autosize="on"></webview>
</div>
  1. http://xxxx/index.html page
    <a href="http://www.baidu.com" target="_blank">_blank</a>
    when target="_blank"锛寃ebview cannot open url; when target="_self" is ok
    Demo_20170413Z64_.zip
P2 bug triaged

Most helpful comment

please refer to https://developer.chrome.com/apps/tags/webview#type-NewWindow/ and below code:

<script type="text/javascript">
function onNewWindow(event) {
  console.log("xiaofeng: ===================="+  event.windowOpenDisposition);

  switch (event.windowOpenDisposition) {
    case 'ignore':
      // Not sure what this is used by.  Default enum value, maybe.
      console.debug('Ignoring new window request');
      return;

    case 'save_to_disk':
      // Ctrl + S, maybe?  Not sure how to reproduce that.
      console.log('save_to_disk is not implemented');
      return;

    case 'current_tab':
      console.log("xiaofeng: ==================== current_tab");
      return;

    case 'new_background_tab':
    case 'new_foreground_tab':
      newWindow = open(event.targetUrl, '_blank');
      if (event.windowOpenDisposition != 'new_background_tab') {
        newWindow.focus();
      }
      break;

    case 'new_window':
    case 'new_popup':
      // if (event.initialWidth && event.initialHeight) {
      //   features = 'width=' + event.initialWidth + ',height=' + event.initialHeight;
      // }
      newWindow = open(event.targetUrl, '_blank');
      newWindow.focus();
      break;
  }
}

  window.onload = function() {
  var webview = document.getElementById('foo');
  console.log("+++++++++++++++++");
  webview.addEventListener('newwindow', onNewWindow);
};
</script>

All 8 comments

I can reproduce this issue on Linux/Windows with nwjs-sdk-v0.21.6.
Steps:

  1. Download the zip file(Demo_20170413Z64_.zip)(modify src to http://127.0.0.1:8080/index.html).
  2. Create index.html with the codes and put it on local http server
<!DOCTYPE html>
<html>
<body>
<a href="http://www.baidu.com" target="_blank">_blank</a>
</body>
</html>
  1. Start http server and launch the app

Result:

target="_blank" --> Fail, no response
target="_self"-->Pass, open baidu page in the same frame

@rogerwang please assign it to me if you are not working on it.

@andy-huaan Chrome app also has the same result and error log:

": A new window was blocked.", source: extensions::webViewActionRequests (84)

@Christywl
I think it's just the "webview" tag limitation instead of a bug.
please refer to http://stackoverflow.com/questions/18428668/how-to-open-a-new-window-from-a-link-in-a-webview-in-a-chrome-packaged-app.

@xzhan96 we should provide a way to remove/control this limitation in NW app.

please refer to https://developer.chrome.com/apps/tags/webview#type-NewWindow/ and below code:

<script type="text/javascript">
function onNewWindow(event) {
  console.log("xiaofeng: ===================="+  event.windowOpenDisposition);

  switch (event.windowOpenDisposition) {
    case 'ignore':
      // Not sure what this is used by.  Default enum value, maybe.
      console.debug('Ignoring new window request');
      return;

    case 'save_to_disk':
      // Ctrl + S, maybe?  Not sure how to reproduce that.
      console.log('save_to_disk is not implemented');
      return;

    case 'current_tab':
      console.log("xiaofeng: ==================== current_tab");
      return;

    case 'new_background_tab':
    case 'new_foreground_tab':
      newWindow = open(event.targetUrl, '_blank');
      if (event.windowOpenDisposition != 'new_background_tab') {
        newWindow.focus();
      }
      break;

    case 'new_window':
    case 'new_popup':
      // if (event.initialWidth && event.initialHeight) {
      //   features = 'width=' + event.initialWidth + ',height=' + event.initialHeight;
      // }
      newWindow = open(event.targetUrl, '_blank');
      newWindow.focus();
      break;
  }
}

  window.onload = function() {
  var webview = document.getElementById('foo');
  console.log("+++++++++++++++++");
  webview.addEventListener('newwindow', onNewWindow);
};
</script>

@Christywl please double check my solution, and close the issue if it's ok.

@xzhan96 , your code works fine, it will open a new window.

this solution operating normally!

Was this page helpful?
0 / 5 - 0 ratings