Javascript Extension function code _ javascript skills that allow firefox to support some IE Methods

Source: Internet
Author: User
Tags javascript extension
Some code can only be implemented in IE, and some extension functions must be used if implemented in firefox. FireFox also supports the innerText method of IE.

The Code is as follows:


Function isIE (){
If (window. navigator. userAgent. toLowerCase (). indexOf ("msie")> = 1)
Return true;
Else
Return false;
}
If (! IsIE () {// firefox innerText define
HTMLElement. prototype. _ defineGetter _ ("innerText ",
Function (){
Var anyString = "";
Var childS = this. childNodes;
For (var I = 0; I If (childS [I]. nodeType = 1)
AnyString + = childS [I]. tagName = "BR "? '\ N': childS [I]. innerText;
Else if (childS [I]. nodeType = 3)
AnyString + = childS [I]. nodeValue;
}
Return anyString;
}
);
HTMLElement. prototype. _ defineSetter _ ("innerText ",
Function (sText ){
This. textContent = sText;
}
);
}


This section makes the HTML element of FireFox 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 ');
}



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 ');
}


Tracking the Enter key event

The Code is as follows:


Function captureKeys (evt ){
Var keyCode = evt. keyCode? Evt. keyCode:
Evt. charCode? Evt. charCode: evt. which;
If (keyCode = 13 ){
// Cancel key:
If (evt. preventDefault ){
Evt. preventDefault ();
}
Var dq = getCookie ('default-engine ');
If (dq = null) dq = "baidu_txt ";
Submit_query (dq );
Return false;
}
Return true;
}

Related Article

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.