Created
March 3, 2011 16:54
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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