click Method for Links (href elements)

Source: Internet
Author: User
Tags html page return

Keywords: javascript, href, click, method

Not all browsers support the click Method for hyperlinks (HREF) on an HTML page.

In IE browser (more than 4.0 versions), we can use the click Method to simulate a hyperlink click action. Like what:

document. links [Somelinkindex].click ();

The click Method first triggers the element's OnClick event, or if the OnClick event returns true, continues to open the URL to which the hyperlink points.

However, in other browsers, such as Ns,firefox, it does not provide such a method. However, you can also add support for the click method to these browsers through the following JavaScript code:

<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>
<bodyONLOAD="Initlinkclick ();">

By loading prototype for the links element in onload, you can implement the Ie/ns-compatible click Method on the page:

<a href=http://www.webjx.com/ onclick= "alert (event.type); return true; " >
Link1</a>
|<a href=http://www.webjx.com/ target= "_blank" >
Link2</a>
|<a href= "http://www.webjx.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>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.