Share some simple JavaScript tips

Source: Internet
Author: User
Tags access properties

Feature detection rather than browser detection

Because a browser does not support a particular feature, it is often our practice to make browser judgments directly in the code such as: Huangmei County Primary School

1 if(Broswer.isFirfox){
2     //do something
3 }

In fact, this is not scientific, because it can be other browsers can support the features you want to use, so, the better way is to directly do the characteristics of judgment. Such as:

1 if(window.localStorage){
2     //使用本地存储功能
3 }
Use square brackets to access properties or methods

This is not mandatory, except that if you use square brackets to access the benefits of some dynamic features, there are many limitations to naming. Such as:

1 for(vari=0;i<9;i++){
2     obj["method"+i]();
3 }
The things that form

Here are two useful properties that will make it easier for you to manipulate the form.

    1. The form has a elements property. The effect is that you can get all the form elements under a form, which is useful when you batch form elements, such as serialization of form elements, or validation.
    2. For the first FORM element there is also a similar property for form. This makes it easy to get a form element that belongs to.
A element of the onclick thing
1 <ahref="cool.html" onclick"doSomething()">做点什么吧</a>

When you click on such a link, there are two situations, 1, if Dosomthing returns True, then the browser will happen to jump to cool.html, return if false, it will ignore the href. So we can write this code in this feature.

1 functiondoSomething(){
2     returnconfirm("您确认要离开吗");
3 }

There is a lot more to find than the code below.

1 functiondoSomething(){
2     varret = confirm("确认要离开吗?");
3     if(ret){
4         window.loaction.href = "cool.html";
5     }
6 }
A different approach to type conversions

How do I convert a string to a number? I know parseint ("123", 10), but there is no good way to look at the following.

View Source print?
1 varnum1 = "123";
2 num1 = num1*1;
3 varnum2 = "123";
4 num2 = +num2;

Both of these methods are not very good. It's still very concise. Wish you a happy study!

1 vara = 1;
2 a = !!a;

Share some simple JavaScript tips

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.