Sometimes, you may want to trigger a link (that is, an anchor element) directly from JavaScript. That is, you may want to simulate the user clicking on the link programmatically.
If you’re using jQuery, you’ll no doubt be aware of the click()
method which can be used to trigger that event on an object. One would think that executing click()
on an anchor element would cause the browser to navigate to the URL, but this isn’t the case. You cannot use jQuery’s click()
method to fully trigger or simulate a user clicking on a link.
Instead, click()
, when executed on links, only seems to trigger any event handlers attached to the DOM element rather than any default behaviour. I’m not sure if this is the case with other event methods or other DOM elements.
The solution
Instead, the solution is to directly manipulate the window.location
object from JavaScript. One would think that since preventing the default action is quite simple (with jQuery’s event.preventDefault()
), triggering the default action of a link click would also be. But this isn’t the case. Here’s a simple example on how to simulate a user clicking on a link.
In this contrived example, we have an ordered list of links that we’ve prevented the default action from executing on each. Instead, to activate a link we’ve provided an input box for the user to input the link number and then hit ‘Go’. When this happens, we grab the identified link and assign the href
attribute value of the link to window.location
, effectively recreating the default action of clicking the link.
HTML Source:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>jQuery Demos</title>
<link rel="stylesheet" href="styles.css" media="all" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="scripts.js"></script>
</head>
<body>
<h1>jQuery Demos</h1>
<ol id="outboundLinks">
<li><a href="http://www.google.com">Google</a></li>
<li><a href="http://www.theglobeandmail.com/">The Globe and Mail</a></li>
<li><a href="http://stackoverflow.com/">Stack Overflow</a></li>
</ol>
<p>
Link Number to activate:
<input id="linkNumber" type="text" size="3" />
<input id="activateLink" type="button" value="Go" />
</p>
</body>
</html>
JavaScript in scripts.js
:
jQuery(function(e)
{
jQuery("#outboundLinks").click(function(e)
{
e.preventDefault();
});
jQuery("#activateLink").click(function(e)
{
var links = jQuery("#outboundLinks li a");
var value = parseInt(jQuery("#linkNumber").val());
if (!isValidNumber(value, links))
{
window.alert("Invalid link number.");
return;
}
else
{
// This won't work. We cannot trigger the default
// behaviour of clicking on a link this way.
// links.eq(value - 1).click();
// Instead, manipulate window.location directly.
window.location = links.eq(value - 1).attr("href");
}
});
});
function isValidNumber(value, links)
{
if (isNaN(value))
{
return false;
}
else if (value <= 0 || value > links.size())
{
return false;
}
else {
return true;
}
}
Note that window.location
is not a regular property; it’s a special object that when assigned to, changes the URL in the address bar of the browser. There’s some debate over whether to directly use the location object or to use the window.location.href
property.
This accomplishes the task of triggering the default action of clicking on a link, albeit in a roundabout fashion. Note that this example will only work for regular links that use href
attributes as the source of the new URL. If you have links doing more complicated things via the use of event handlers (i.e. Ajax), then you’re better off using click()
or directly invoking the JavaScript methods involved.
OMG I have been searching for triggering links in javascript for freakin ages! Thank you so much, this makes my job a lot easier
how about “document.referrer”??? IE does not pass referrer if request was not originated by ‘link’ or on a server.
Thank you for good content na.