0

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.

2
  • 1
    What is the goal here? Are you trying to open a url? Commented Mar 24, 2015 at 20:50
  • It's written in the beginning of the post: "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.". So, yes, i want to open a link. Commented Mar 24, 2015 at 20:58

1 Answer 1

1

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')");
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the feedback; it's strange because the click() method that i'm using with getElementsById is correctly working with every user agent i'm using. Anyway, yes, i need to open the link but, in the meantime, i need that the Javascript on the page i'm clicking on will register the click; if i proceed with the window.open, i just copy the link and open it in a new browser, without any register in the page.
Are you using analytics or something? Do you control all of the code on the page? Or is this some kind of hack? I'm still not sure why you need the click event to fire.
I need the click event because i need my app clicks on the Google Search result. The app i'm building it's a kind of personal version of the "I'm feeling lucky" where i show the user the app is searching on google simulating the same action of a human research (but the user cannot interact - he can only see all the phases of the research). So, as it's a human simulation, it has to work like a human activity!
Your goal is still unclear... do you need the link to change color, as it does when a user clicks?
Yes, this could be a short explanation
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.