Entry
Is there a click method for links (A HREF elements)?
Aug 15th, 2006 07:19
DKL T, comma geneus, Martin Honnen,
With IE4/5 links have a click method simulating executing a link e.g.
document.links[someLinkIndex].click()
first executes the link's onclick handler and if that returns true
executes (loads) the link's href.
(revised Aug06)
NN2/3/4 don't provide a click method for links however. The following
code (method 1) remedies that for NN4 by setting up a click method for
links
implemented with JavaScript. Method 2 below is another workaround.
=== method 1 ====
<SCRIPT LANGUAGE="JavaScript1.2">
function linkClick () {
var executeAction = true;
if (this.onclick) {
executeAction = this.onclick({type: 'click'});
}
if (executeAction)
open (this.href, this.target ? this.target : '_self');
}
function initLinkClick () {
if (document.layers && document.links.length > 0)
document.links[0].constructor.prototype.click = linkClick;
}
</SCRIPT>
</HEAD>
<BODY ONLOAD="initLinkClick();">
With that code included the following example buttons calling the links
click method should work the same in NN4 and IE4/5:
<A HREF="http://www.kibo.com" ONCLICK="alert(event.type); return true;">
link1
</A>
|
<A HREF="http://www.faqts.com" TARGET="_blank">
link2
</A>
|
<A HREF="http://www.faqts.com" ONCLICK="alert(event.type); return
false;">
link3
</A>
|
<BR>
<FORM>
<INPUT TYPE="button" VALUE="link1 click"
ONCLICK="document.links[0].click();"
>
<INPUT TYPE="button" VALUE="link2 click"
ONCLICK="document.links[1].click();"
>
<INPUT TYPE="button" VALUE="link3 click"
ONCLICK="document.links[2].click();"
>
</FORM>
=== method 2 ====
// on timeout or after some logic processing, do a javascript redirect
// which will behave exactly as if a user left click on a <A> link
window.location=document.links[0].href;