Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-27644] Android: Webview opens new window with webview if link target is "_blank"

GitHub Issuen/a
TypeBug
PriorityCritical
StatusOpen
ResolutionUnresolved
Affected Version/sRelease 8.3.0
Fix Version/sn/a
ComponentsAndroid
LabelsengTriage
ReporterHans Knöchel
AssigneeYordan Banev
Created2019-12-06T19:58:13.000+0000
Updated2019-12-09T16:32:57.000+0000

Description

On Android, a webview opens new window with webview if link target is "_blank". This does not happen on iOS and should not happen except the developer explicitly wants a new window. Example:
var win = Ti.UI.createWindow({ title: 'Test Window' });
var webView = Ti.UI.createWebView({
    html: '<html><body><a href="https://google.com" target="_blank">Click me!</a></body></html>'
});
webView.addEventListener('beforeload', function (event) {
    if (event.url.includes('google.com')) {
        alert('STOP LOADING');
        webView.stopLoading();
    }
});
win.add(webView);
win.open();
Any workaround is appreciated. The blacklistedURLs property also does not help. *EDIT*: There is an old [onCreateWindow](https://docs.appcelerator.com/platform/latest/#!/api/Titanium.UI.WebView-property-onCreateWindow) property which is meant to handle _blank links, but unfortunately, returning null there does also not stop the new window from popping up. Same for returning false in the onLink property.

Comments

  1. Michael Gangolf 2019-12-06

    I think this should help: http://docs.appcelerator.com/platform/latest/#!/api/Titanium.UI.WebView-property-onCreateWindow
       webView.onCreateWindow = function(e) {
       	return null; // don't open popup links
       };
       
    or expose this property: https://github.com/appcelerator/titanium_mobile/blob/13658500189a8de1c8fa9773518fbe0ee7947036/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiUIWebView.java#L318 and set it to false (which would be a good idea)
  2. Hans Knöchel 2019-12-06

    Just tried that and unfortunately it does not work. Disabling the above property will probably help, will build a custom SDK again. For parity with iOS, it should even be disabled by property, preventing multiple windows from being opened. *EDIT*: Holy moly. Assigning the property after creation works. De-escalated, thanks Michael! But hopefully this can be properly fixed in 9.0.0. *EDIT 2*: Unfortunately this does also not work, because the beforeload event does not fire anymore at all.
  3. Michael Gangolf 2019-12-06

       var win = Ti.UI.createWindow({
       	title: 'Test Window'
       });
       
       var webView = Ti.UI.createWebView({
       	html: '<html><body><a href="https://google.com" target="_blank">external link</a> - <a href="https://google.de" >internal link</a></body></html>'
       });
       
       webView.addEventListener('beforeload', function(event) {
       	if (event.url.includes('google.com')) {
       		alert('STOP LOADING');
       		webView.stopLoading();
       	}
       });
       
       var tmpWebView = Ti.UI.createWebView({
       	visible:false
       });
       
       webView.onCreateWindow = function(e) {
       	return tmpWebView;
       };
       
       win.add(webView);
       win.open();
       
    That will trigger the alert for the external link.
  4. Hans Knöchel 2019-12-06

    Mhhh, but what to do with the webview? I want to handle the URL with an external browser and return to the app, so I feel this could lead into a memory leak. *EDIT*: Manually disabling the native property doesn't help btw. I compiled a new SDK and it works with the first click and fails for all additional clicks.

JSON Source