Search code examples
titaniumappceleratortitanium-mobile

open URL link inside webview in mobile browser in Titanium Appcelerator


I have a webview with HTML contents. And I have few links in that HTML content. when i clicked the link,corresponding webpage opens within my application. Can i open the webpage in mobile browser when i clicked a particular link?

I have tried using target="_blank" property within anchor tag,which doesn't works. and also i have used "openURL" webview property, but fireEvent is not working properly.

my main js file;

var mywebview = Ti.UI.createWebview({
height : 'auto',
width : '100%',
html : '<!DOCTYPE html><html><body><a href="#" target="_blank" onclick="Ti.App.fireEvent("openLink", {linkUrl: "http://www.w3schools.com"});">Visit W3Schools.com!</a><br><a href="#" onclick="Ti.App.fireEvent("openLink", {linkUrl: "http://google.com"});">Visit Google.com!</a></body></html>',
)};

In app.js;

Ti.App.addEventListener("openLink", function(e){
    alert(e.linkUrl);
    Ti.Platform.openURL(e.linkUrl);
});

Solution

  • Ti.App.fireEvent works for me in this form:

    <a href="#" onclick=" Titanium.App.fireEvent('ynOpenURL', {url:'http://youatnotes.com'}); return false;">youatnotes.com</a>
    

    and in app.js:

    Ti.App.addEventListener('ynOpenURL', function(e) {
            Ti.Platform.openURL(e.url);
        });
    

    You could the URL of the webview in the event in order to open the link inside the WebView.