open link in safari onclick within webview
is this possible to use for opening a new safari window, if a link within webview-html-content is clicked?
i tried to use
onclick="Titanium.Platform.openURL('www.url.com');"
within an <a>-tag
and failed, but maybe i'm doing something wrong here
6 Answers
-
Accepted Answer
Hi!
To my understanding, only the following are available in a webview:Titanium.App.addEventListener
Titanium.App.removeEventListener
Titanium.App.fireEvent
Titanium.API.*Therefore you cannot call
Titanium.Platform.openURL('www.url.com');
directly. You'll have to bridge it over to the native side, something like this:webview.html link attribute
onclick="Ti.App.fireEvent('openURL', { url: 'http://www.google.com'});"
app.js
Ti.App.addEventListener('openURL', function(e){ Ti.Platform.openURL(e.url); });
Cheers
/Jacob -
Any idea why it might not be working with Android? I have the following headers in a webview:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <script type="text/javascript"></script> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="viewport" content="initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" /> </head>
the following link in the webview:
<a href="#" onclick="Ti.App.fireEvent('openURL', { url: 'http://www.google.com'}); return false;">Google</a>
and the following listener in the beginning of app.js:
Ti.App.addEventListener('openURL', function(e){ Ti.Platform.openURL(e.url); });
What I also noted, is that it's not even entering into this event listener when was trying to debug. Is this trick not available for Android?
I'm on Titanium Mobile SDK Version: 1.8.2; Titanium Studio: 1.0.9.
-
I've only been able to get to a web site through creating a webview. You can create a button that will open the web view. See kitchenSink for web view examples. Here's how to get the web view going:
var win5 = Titanium.UI.createWindow({ Title:'WEB VIEW', barColor: '#4b4b4b' }); var webview = Titanium.UI.createWebView({ url:'http://www.foo.com/foo.html' }); win5.add(webview);
-
So it's not possible to do this only within webview?
-
Accepted Answer works fine with latest sdk
-
Florian's post did work me as well. Slight modification
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> do stuff </body> </html>