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
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){ |
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:
The things that form
Here are two useful properties that will make it easier for you to manipulate the form.
- 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.
- 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 |
< a href = "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.
2 |
return confirm( "您确认要离开吗" ); |
There is a lot more to find than the code below.
2 |
var ret = confirm( "确认要离开吗?" ); |
4 |
window.loaction.href = "cool.html" ; |
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?
Both of these methods are not very good. It's still very concise. Wish you a happy study!
Share some simple JavaScript tips