tags:

views:

5910

answers:

9

Hello is there any way to automatically click on a link on my page using javascript?

+1  A: 

You can't make the user's mouse do anything. But you have full control over what happens when an event triggers.

What you can do is do a click on body load. W3Schools has an example here.

Ólafur Waage
hmm, you're right, maybe he is asking for this..
Canavar
A: 

Simply like that :

<a id="myLink" onclick="alert('link click');">LINK 1</a>
<a id="myLink2" onclick="document.getElementById('myLink').click()">Click link 1</a>

or at page load :

<body onload="document.getElementById('myLink').click()">
...
<a id="myLink" onclick="alert('link click');">LINK 1</a>
...
</body>
Canavar
Yes but then some other click has to happen.
Ólafur Waage
Can't you do it on any event, like page load?
Bill the Lizard
yes you're right I thought he was asking for on click of another link..
Canavar
thanks for help, but im using firefox 3 and it doesn't work, in error console says Error: document.getElementById("myLink").click is not a function
but clicking on link works, just .click isn't working
It seems like firefox doesn't support click method. and most of the answers, including mine, are not helpful :)
Canavar
A: 

Instead of clicking, can you forward to the URL that the click would go to using Javascript?

Maybe you could put something in the body onLoad to go where you want.

Jas Panesar
A: 

You can use jQuery to easily accomplish this. Just set a delay when the document loads, and have it set the document.location property to the href of your link element:

// Using jQuery
$(document).oneTime(1000, function()
{
   document.location = $('myLink').attr('href');
};

EDIT: Turns out .Click() doesn't actually follow the link. The example has been updated to fix the problem. ;) The example above has been tested and does indeed work.

jrista
this will only call any event handlers on the link - it won't perform the default browser action: ie: go to the url specified in href.
nickf
Good catch. I've updated the script to function properly (and tested it to verify.)
jrista
+4  A: 

If you only want to change the current page address, you can do that by simply doing this in Javascript :

location.href = "http://www.example.com/test";
Fabien Ménager
+1  A: 

You could just redirect them to another page. Actually making it literally click a link and travel to it seems unnessacary, but I don't know the whole story.

akway
+4  A: 

This function works in at least Firefox, and Internet Explorer. It runs any event handlers attached to the link and loads the linked page if the event handlers don't cancel the default action.

function clickLink(link) {
    var cancelled = false;

    if (document.createEvent) {
        var event = document.createEvent("MouseEvents");
        event.initMouseEvent("click", true, true, window,
            0, 0, 0, 0, 0,
            false, false, false, false,
            0, null);
        cancelled = !link.dispatchEvent(event);
    }
    else if (link.fireEvent) {
        cancelled = !link.fireEvent("onclick");
    }

    if (!cancelled) {
        window.location = link.href;
    }
}
Matthew Crumley
Matthew is correct, and http://stackoverflow.com/questions/809057/how-do-i-programmatically-click-on-an-element-in-firefox has some more info about why. Cross browser is fun :)
Dan F
A: 
document.getElementById('yourLinkID').click();
arik-so