Skip to content

Instantly share code, notes, and snippets.

@dawsontoth
Created March 21, 2011 16:51
Show Gist options
  • Save dawsontoth/879768 to your computer and use it in GitHub Desktop.
Save dawsontoth/879768 to your computer and use it in GitHub Desktop.
The following snippet takes over links in a webview of a remote URL, 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 (li
var win = Ti.UI.createWindow({ backgroundColor: '#fff' });
var web = Ti.UI.createWebView({
url: 'http://www.appcelerator.com/'
});
var linkJS = 'document.titaniumLinkQueue = [];'
+ '(function(){'
+ 'var links = document.getElementsByTagName("a");'
+ 'for(var i = 0, l = links.length; i < l; i++) {'
+ 'var h = links[i].attributes["href"];'
+ 'h.value = "javascript:document.titaniumLinkQueue.push(\'" + h.value + "\');"'
+ '}'
+ '})();';
web.addEventListener('load', function() {
web.evalJS(linkJS);
// and 3 times a second, check to see if any links have been clicked
setInterval(pollClickedLinks, 333);
});
function pollClickedLinks() {
var link = web.evalJS('document.titaniumLinkQueue && document.titaniumLinkQueue.pop();');
if (link) {
Ti.App.fireEvent('linkClicked', { href: link });
}
}
Ti.App.addEventListener('linkClicked', function(evt) {
alert('You clicked: ' + evt.href);
});
win.add(web);
win.open();
@LucitheR
Copy link

Sorry to say that does not work on android 2.3.3. nor 4.0.3. link is undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment