How do I programmatically click a link with javascript? How do I programmatically click a link with javascript? javascript javascript

How do I programmatically click a link with javascript?


document.getElementById('yourLinkID').click();


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;    }}


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";