Created
March 21, 2011 16:51
-
-
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
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 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(); |
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
If the windows is not opened with the "url" property, clear the setInterval when closing the window, otherwise it will go for ever