I confirmed that click is not a W3C method for anchor objects. However, thanks to the prototype property of objects you can extend a host object by adding methods to it. The code below does this for anchor elements, although you could do it for any element. Credit to Martin Honnen for his article at http://www.faqts.com/knowledge_base/view.phtml/aid/9368 for the solution. Here’s the code, which just needs to be in the top of your page so that it runs prior to any scripts at document load time:


try {
// create a element so that HTMLAnchorElement is accessible
document.createElement('a');
HTMLElement.prototype.click = function () {
if (typeof this.onclick == 'function') {
if (this.onclick({type: 'click'}) && this.href)
window.open(this.href, this.target ? this.target : '_self');
}
else if (this.href)
window.open(this.href, this.target ? this.target : '_self');
};
}
catch (e) {
alert('click method for HTMLAnchorElement couldn\'t be added')
}

Leave a Reply