Skip to content

Instantly share code, notes, and snippets.

@dawsontoth
Created March 3, 2011 16:54
Show Gist options
  • Save dawsontoth/853084 to your computer and use it in GitHub Desktop.
Save dawsontoth/853084 to your computer and use it in GitHub Desktop.
The following snippet takes over links in webviews, causing them to do nothing but Ti.App.fireEvent('linkClicked', { href: 'http://example.com' }). This lets you load in web pages, and control what should happen when a link is clicked.
var win = Ti.UI.createWindow({ backgroundColor: '#fff' });
var html = '<html><body>'
+ '<a href="http://pedro.com">Pedro</a> '
+ '<a href="http://is.com">is</a> '
+ '<a href="http://a.com">a</a> '
+ '<a href="http://balla.com">balla!</a>.'
+ '</body></html>';
var web = Ti.UI.createWebView({
html: html
});
var linkJS = '(function(){var a=document.getElementsByTagName("a");'
+ 'for(var i=0,l=a.length;i<l;i++){'
+ 'h=a[i].attributes["href"];'
+ 'h.value="javascript:Ti.App.fireEvent(\'linkClicked\',{href:\'" + h.value + "\'})"'
+ '}'
+ '})();';
Ti.App.addEventListener('linkClicked', function(evt) {
alert('You clicked: ' + evt.href);
});
web.addEventListener('load', function() {
web.evalJS(linkJS);
});
win.add(web);
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment