Myths About jQuery
Myths About jQuery
Here are some of the Myths About jQuery
- Not using the latest version
Do you see if your machine is up to date? Maybe you will not update the website that you have already done. After all, no one will pay you for this workload, but what about new development projects? Don’t forget to use the latest version. After all, each version update will definitely improve the function or performance, or fix a few bugs, but sometimes the inertia makes people don’t want to study the changes in the new version. Therefore, remind you not to forget that it is better to use the new one in the new project than the old version.
- jQuery without CDN-hosted
Many people like to download to their server, and then call. It seems that subconsciously it is safer to put the files in your own hands. But most of the time, unless you are particularly cattle, your own server cannot match the CDN server. If the traffic is small, there may be no impact, but if the traffic is large, the performance impact is very prominent. At this time, you can consider the CDN server, such as Google’s Copy, which has a great impact on the performance of the website’s help!
You only need to use the following when calling:
1 script type = “text/javascript” src = “//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js”> </script>
Note: If the link to google has expired, will not be able to refer to the jquery core library, you can find the available host by yourself.
- Used CDN-hosted version but no backup measures
Although we believe that Google is very good and will not cause problems, don’t forget that you are in the heavens, everything is possible! Therefore, it is still necessary to add the disaster recovery plan of the local server version to the HTML code, and it is also very simple:
1 <script src=”//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js”></script>
2 <script>window.jQuery || document.write(‘<script src=”js/libs/jquery-1.7.2.min.js”><\/script>’)</script>
- Forget about chaining
When we perform some common operations, if we use more chain operation methods, we can improve performance without loading elements multiple times. Sometimes the performance improvement brought by chain writing is quite considerable. E.g
1 $ (“# mydiv”). Hide (); 2 $ (“# mydiv”). Css (“padding- left”, “50px”); 3 JQuery will get the element 2 times 4 $ (“# mydiv”) .hide (). css (“padding-left”, “50px”);
- Forget caching
This is an important tip related to performance: don’t forget to cache! If an element is called at least twice on the page, you should consider caching. A variable can be used to cache selectors in JQuery. When using the selector for the second time, you only need to reference the variable. The advantage of this is that jQuery does not need to traverse the entire DOM tree again to find the element you selected. E.g
1 var $myMxria =$ (“# mydiv”). Hide (); 2 //……
3 $myMxria.show();
- Why not use original JS?
If you are used to $, you basically ignore the document.getElementById, but you have to say that it is wrong to abuse $, why? If it is purely attributed modification, the original JavaScript is better, and no jQuery object is used at all. For example, in the following example, the browser will still convert the jQuery object into a DOM node for operation.
1 $ mydiv [0] .setAttribute (‘class’, ‘awesome’);
- Abuse plug-ins without checking
Today, the popularity of JQuery also brings countless plug-ins, and there are many wonderful and thrilling plug-ins. We like to follow the trend and use some fashionable things. But in fact, many plug-ins need our careful evaluation to see if it is the right project for you, or do you really need it? Have you considered the following factors?
File size: over 10k, you have to think twice
Performance evaluation: Convenience does not matter performance? Really?
Cross-browser support: See what browser your customers are using, don’t forget the old IE6, but the style is still the same
Mobile support: Confirm that the plug-in can work normally in the Mobile environment.
- Without jQuery
Is it okay? Why do you have to use it? Many times we may not have thought about this problem at all, the first thought may be that it seems easy to implement with jquery. This misunderstanding is really common. Do you have the habit of cutting it with a sharp knife?
- Use jQuery to do server-side work
JQuery is too good, too powerful, it seems that everything can be done on the front end! Therefore, we will enter a misunderstanding: similar to the effectiveness of validation and data verification, the importance of the server is ignored. A lot of business logic can be processed in the front-end. The ancient brain is processed in the front-end. Remember, anything can happen in the front-end browser!! ! Disabling JS, a certain incompatibility problem, forging data, and attacking behaviors are not 100% controllable by developers, so don’t relax your vigilance because of the power of jQuery!