Forum Moderators: open

Message Too Old, No Replies

clicking links with javascript?

is it possible to invoke javascript click method on links?

         

michelek

7:20 pm on Sep 17, 2007 (gmt 0)

10+ Year Member



what could be a working equivalent for

...
var mylink = document.getElementsByTagName("a");
myLink.click();
...

penders

12:53 am on Sep 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



var mylink = document.getElementsByTagName("a");

This returns an array of all the anchors in the document, so to call the onclick event of the first anchor, you could call:

mylink[0].onclick();

But I have a feeling you don't mean that. Are you wanting to visit the URL as specified in the href attribute? Then you could try:

location.href = mylink[0].href;

If it's not a straight forward URL in the href attribute (it could be a call to a JavaScript function) then it's going to require a little more work. As far as I'm aware there is no direct way to simulate a user clicking on a link, without first parsing the element.

michelek

12:15 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



thanks for correcting, mylink[0] is correct, of course.
my intention is not to follow a link, question is if one could "click" on a link with help of a javascript.

I have some local greasemonkey scripts, that are ment to assist in browsing the web and in some situations I'd want to be able to click automagically.

Unfortunately I can't assume, that links are all URLs, because lot of them are calling for inline, even generated javascript that is not accessible globally.
So if there is no way to simulate a click, then I'll go for parsing...

thanks-a-lot

michelek

4:53 am on Sep 19, 2007 (gmt 0)

10+ Year Member



like, You can invoke a submit on a form
...
var myForm = document.getElementsByTagName("form")[0];
myForm.submit();
...

penders

7:43 am on Sep 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As far as I'm aware there is no direct way to simulate a user clicking on a link, without first parsing the element.

Actually I could have been very wrong there... it looks like you can do it?! I've not tried it... would also need to check cross-browser compatibility if that is of concern.

Try simply:

document.getElementById('mylink').click()

Check these threads (from a few years ago!):
[webmasterworld.com...]
[webmasterworld.com...]

penders

8:34 am on Sep 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Browser compatibility is a problem...

As mentioned in the above threads, this works OK in IE6 and Opera, but it doesn't seem to work in FF2 (ERROR: .click is not a function). Is there an alternative method for Firefox?

michelek

8:50 am on Sep 19, 2007 (gmt 0)

10+ Year Member



oh, shame on me, I really should have searched harder for earlier threads about subject. I actually did some search, but Im no good at it.

Looks like click() method actually works for some browsers (IE at least) , but I was hoping to use it in Firefox greasemonkey scripts.

Javascript click() method is documented at least on javascriptkit.com under DOM Element methods - might it be IE-only feature and for security reasons not supported by others?

Achernar

9:49 pm on Sep 19, 2007 (gmt 0)

10+ Year Member Top Contributors Of The Month



Instead of:
element.click()
try:
if (!element.onclick ¦¦ (element.onclick()!=false)) location=element.href

Note: it will not fire events set with addEventListener().