Add event listener to links loaded in webview? (Ti

2019-08-29 06:50发布

问题:

I am working on a sample app using titanium. I have created a webview and loaded a local html as shown below

var webview = Ti.UI.createWebView({ borderWidth:0, paddingRight:10,width:310,top:25, height:210,left:5 });
webview.html = '<div><a href="http://google.com" id="ggle"></a></div>'

is it possible for me to add event listener for the anchor tag specified in the html? if so how? if not please suggest me anyother possible solution.

thanks.

回答1:

Yes you can add your custom event like this:

<div><a href="http://google.com" id="ggle" onclick="Ti.App.fireEvent('openLink', {linkUrl: 'http://google.com'});"></a></div>

And listen to the event in your app.js file:

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

For complete details Communication Between WebViews and Titanium