The onkeyup event of firefox2.0 encountered during Ajax Testing

Source: Internet
Author: User
VaR XMLHTTP;
Function createxmlhttprequest (){
If (window. activexobject ){
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest ){
XMLHTTP = new XMLHttpRequest ();
}
}
Function validemail (e ){
Createxmlhttprequest ();
VaR ename = Document. getelementbyid ("ename ");

When learning Ajax, I wrote a piece of code:

<Form action = "" method = "Post">
<Table border = "1" width = 100%>
<Tr> <TD Height = 30>
Username: <input type = "text" name = "ename" id = "ename" onkeydown = "validemail (E);">
</TD> </tr>
<Tr> <TD Height = 50>
<Span id = "show"> </span> </TD> </tr>
</Table>
</Form>

 
<SCRIPT type = "text/JavaScript">
VaR url = "example6-2a.php? Ename = "+ ename. value;

XMLHTTP. onreadystatechange = callback;
XMLHTTP. Open ("get", URL, true );
XMLHTTP. Send (null );
}
Function callback (){
If (XMLHTTP. readystate = 4 ){
If (XMLHTTP. Status = 200 ){
Document. getelementbyid ("show"). innerhtml = "server information:" + XMLHTTP. responsetext;
}
}
}
</SCRIPT>

However, when running on the current machine (firefox2.0 under FreeBSD), it does not get the expected effect and cannot be displayed when you enter characters (server information :****), accessing my server on another machine can work normally with IE or FF (3.0. I searched for it and did not get a satisfactory answer. There is an explanation that may partially explain the problem:

 

Compatibility with onkeyup in IE and Firefox

When creating a search function, the submit button is replaced by an image. In this case, it is easy to implement the function to automatically trigger the submission after the user enters a keyword and press Enter, however, Firefox later found that. the keycode does not catch a cold. After a Google click, the function key value returned by keycode in Firefox is not supported, as shown below:

In ie:
Keycode is supported, and which and charcode are not supported. Their values are undefined.
In Firefox:
Keycode is supported. Except for function keys, the value of other keys is always 0, and which and charcode are supported. The values are the same.

The solution is as follows:

View Source Code Javascript
function EnterSubmit(){
var _key;
document.onkeyup = function(e){
if (e == null) { // ie
_key = event.keyCode;
} else { // firefox
_key = e.which;
}
 
if(_key == 13){
s_submit();
}
};
}

I think it may be a problem with the browser version. If you have encountered a similar problem or know the cause, please explain. Thank you.

 

 

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.