Reputation: 389
I'm injecting a Javascript code in a webpage (opened in a webview) in order to let the app click on a URL link and open the page. I'm using the following code:
myBrowser.loadUrl("javascript:document.getElementsByTagName('a')[0].click();");
But it's not working; it's giving the following error: "Uncaught TypeError Object [object] has no method 'click' at null:1".
I can't understand where the problem is because i inject other Javascript in the same page (in another part of the code) through the getElementsByID("word").click() and it's correctly working.
I tried different user agents but nothing changed.
The target SDK is 14 and the minimum SDK is 9.
Someone told me that the .click() method is not supported by getElementsByTagName but it's not correct; i tried the same code on the "Try It Yourself editor" and it's correctly working.
Thanks for your support.
Upvotes: 0
Views: 1163
Reputation: 9471
Support for triggering native click events is flaky across browsers, especially mobile.
Since you're just trying to open a window, use window.open()
.
https://developer.mozilla.org/en-US/docs/Web/API/Window/open
Edit...
If you want to fake a click, create a js function that is accessible from the window and call it from the android app.
window.fakeClick = function(anchorSelector){
var el = document.querySelector(anchorSelector);
el.style.color = "red"; // fake active state
ga.post(...) // send click to analytics somehow
window.open(anchorSelector.href); // open the link
}
in android...
myBrowser.loadUrl("javascript:window.fakeClick('a.someclass')");
Upvotes: 1