Javascript simulated Click Event (Click link and html click) compatible with IE/Firefox_javascript skills

Source: Internet
Author: User
Javascript simulates click events, which are generally supported by ie, but not by firefox. Therefore, we can solve this problem through the following methods. In one case, two aspects are simulated: click a super connection event.
Firefox compatible Functions
Add an onclick event to HTMLAnchorElement

The Code is as follows:


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.tar get? This.tar get: '_ self ');
}
Else if (this. href)
Window. open (this. href, this.tar get? This.tar get: '_ self ');
};
}
Catch (e ){
// Alert ('click method for HTMLAnchorElement couldn \'t be added ');
}


The following are specific applications:

<Script type = "text/javascript"> try {document. createElement ('A'); HTMLElement. prototype. click = function () {if (typeof this. onclick = 'function') {if (this. onclick ({type: 'click'}) & this. href) window. open (this. href, this.tar get? This.tar get: '_ self');} else if (this. href) window. open (this. href, this.tar get? This.tar get: '_ self') ;};} catch (e) {}var url = location. href; var urllink = "http://www.jb51.net/do/plus/sh_down.php? Url = "+ url; document. write ("anti-leech has been enabled for the script home. download it from here"); var myLink = document. getElementById ("jb51link"); myLink. href = urllink; myLink. click (); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


If it is a common html add click
This section makes FireFox's HTMLElement have the click method (add click method to HTMLElement in Mozilla)

The Code is as follows:


Try {
// Create span element so that HTMLElement is accessible
Document. createElement ('span ');
HTMLElement. prototype. click = function (){
If (typeof this. onclick = 'function ')
This. onclick ({type: 'click '});
};
}
Catch (e ){
// Alert ('click method for HTMLElement couldn \'t be added ');
}


The following are other articles related to netizens.
Recently, I found that it is not fixed for users to press enter in the webpage input box...
Especially when a webpage has multiple forms
So I searched for a js with a simulated click. It was tested to run on firefox and ie.

The Code is as follows:


Function doClick (linkId, e ){
If (e. keyCode! = 13 ){
Return;
}
Var fireOnThis = document. getElementById (linkId)
If (document. createEvent)
{
Var evObj = document. createEvent ('mouseevents ')
EvObj. initEvent ('click', true, false)
FireOnThis. dispatchEvent (evObj)
}
Else if (document. createEventObject)
{
FireOnThis. fireEvent ('onclick ')
}
}


E is an event, a built-in object, and a linkId is the id of the simulated clicked object.
For example
In this way, you can press enter to submit the form ~
Opera can be changed.

The Code is as follows:


Click me


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.