JS errors encountered at work (supplement at any time)

Source: Internet
Author: User

In the work, writing JS often encountered some errors that do not meet the expectations, hereby recorded:

1. Firefox and Google about the KeyCode event compatibility: wrote a JS control input box can only enter numbers, but found in Google Browser can be used, but in Firefox is not allowed to enter the number.

    

function (event) {            varnull);             var keycode=E.keycode;             if ((keycode >= && keycode <=) | | keycode = 8) {            else  }                  Returnfalse;            }        })

This code in Firefox, it is not possible to enter the number, and then found that each output keycode found to be 0, the feeling of getting keycode error, and then check the data found that Firefox KeyCode and Google's acquisition method is not the same. So the code is modified to:

 $ (". Money"). Live ("KeyPress", function   ( Event) { var  e = event? Event:(window.event?            Window.event: null  );             var  keycode=0;  //  Here are different  keycode=e.keycode| | e.which| |            e.charcode;  if  ((keycode >= && keycode <=) | | keycode = 8" {}  else   {  return  false  ; }        })    

In this way, Google and Firefox can be compatible with the number of input restrictions. Of course, if the paste copy in the past can not be controlled, so after the loss should also do a check.

2. About JS Digital size comparison problem:

    

1 var first = ' 1234 '; 2 var = ' second '; 3 Console.log (first < second); True

This is because this is a string comparison, according to a letter-by-letter comparison, comparing the first letter 1, equal, the second letter 3>2, so true;

Usually use jquery to get the value of the input box is actually this form if the direct comparison is definitely wrong. Need to be replaced by a digital comparison

var first = ' 1234 '; var = ' second '/ / true/ / equals to var first = 1234; var second = PPI//true

JS errors encountered at work (supplement at any time)

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.