jQuery 开发者应该注意的9个错误


Posted in Javascript onMay 03, 2012

jQuery is so easy to use that sometimes we just forget that it's not CSS. While using CSS, we don't have to give much thought to performance, because it's so fast that it's not worth the effort to optimize it. But when it comes to the real world, jQuery can drive developers crazy with performance issues. Sometimes you lose precious milliseconds without even noticing it. Also, it's so easy to forget about some functions and we keep using the old (and not-so-good) ones.

Let's take a look at a few of the most-common-and-easiest-to-fix mistakes while using jQuery in your projects.

1. You aren't using the latest jQuery version
Each version update means higher performance and several bug fixes. The current stable release is 1.7.2, and I'm pretty sure you know about plenty of sites developed using 1.6 and below. Ok, ok, you can't just update every old site for every jQuery update (unless your client is paying you to do so) but you should at least start using it for your new projects. So, forget about this local copy and just grab the latest release every time you start a new project.

2. You aren't using a CDN-hosted copy of jQuery
How many unique visitors you`ve got last month? I bet the number is still under 1 billion, right?
So you'd better use Google's copy of jQuery instead of yours. If your user still has the cached file of Google's website (or from many other sites that uses its CDN) his browser will just get the cached version, improving a lot your website's performance. You can use it by copying & pasting this HTML:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

3. You aren't using a fallback for CDN version
I know I said Google is awesome and stuff, but they can fail. So, every time you rely upon external sources, make sure you have a local fallback. I've seen this snippet in the HTML5 boilerplate source code and just found it amazing. You should use it too:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.7.2.min.js"><\/script>')</script>

4. You aren't chaining stuff
While doing common operations, you don't need to call the element every time you want to manipulate it. If you're doing several manipulations in a row, use chaining, so jQuery won't need to get the element twice.

Instead of this:

$(“#mydiv”).hide();
$(“#mydiv”).css(“padding-left”, “50px”);

Use this:

$(“#mydiv”).hide().css(“padding-left”, “50px”);

5. You aren't caching stuff
This is one of the most important performance tips. If you'll call an element at least twice, you should cache it. Caching is just saving the jQuery selector in a variable, so when you need to call it again you'll just reference the variable and jQuery won't need to search the whole DOM tree again to find your element.

/* you can use it this way because almost every jQuery function returns
the element, so $mydiv will be equal to $(“#mydiv”); also it's good to
use the $mydiv so you know it's a jQuery caching var */

var $mydiv = $(“#mydiv”).hide();
[.. lot of cool stuff going on here …]
$mydiv.show();

6. You aren't using pure js
Specially for attributes modification, we have several built in methods to get stuff done with pure javascript. You can even “convert” jQuery objects back to DOM nodes to use them with simpler methods, like this:

$mydiv[0].setAttribute('class', 'awesome'); //you can convert jQuery objects to DOM nodes using $jqObj[0]

7. You aren't checking plugins before including in your site
You know, jQuery is not the hardest thing in the world to code. But good JS (and jQuery), that is pretty hard. The bad news is that while you aren't a good programmer, you'll have to rely on trial and error to find out what is good and what isn't. A few points you must be aware of while including a plugin in your project:

File Size (the easiest to check!) ? if a tooltip plugin is bigger than jQuery source, something is really wrong;
Performance ? You can try it with firebug and others. They give you easy to understand charts to you'll know when something is out of place;
Cross-browsing ? Test, at least on IE7, but Chrome, Firefox, Safari and Opera are good ones to try also
Mobile ? Don't forget that everything is getting mobile. See if the plugin works, or at least doesn't crash your mobile browser
8. You aren't open to remove jQuery
Sometimes it's just better to remove it and use simple ol' CSS. Actually if you want, for instance, an opacity hover, or transition can be done with CSS along with good browser support. And there's no way jQuery can beat plain CSS.

9. You are using jQuery for server side tasks
I know that masking and validating are good, but don't rely just on jQuery for those. You need to recheck everything on the server side. That is especially important if you are thinking about using AJAX. Also, make sure everything will work with JS disabled.

So, it's your turn!

Do you have anything you think should be on this list? Share your thoughts!

About the Author

Javascript 相关文章推荐
Javascript函数的参数
Jul 16 Javascript
javascript实现数组中的内容随机输出
Aug 11 Javascript
js获取时间精确到秒(年月日)
Mar 16 Javascript
js阻止浏览器默认行为的简单实例
May 15 Javascript
jqueryMobile 动态添加元素,展示刷新视图的实现方法
May 28 Javascript
jQuery实现的购物车物品数量加减功能代码
Nov 16 Javascript
JavaScript获取服务器时间的方法详解
Dec 11 Javascript
简单实现Bootstrap标签页
Aug 09 Javascript
Node.js调试技术总结分享
Mar 12 Javascript
JS库之Three.js 简易入门教程(详解之一)
Sep 13 Javascript
浅谈webpack打包之后的文件过大的解决方法
Mar 07 Javascript
Angular6笔记之封装http的示例代码
Jul 27 Javascript
jQuery Ajax请求状态管理器打包
May 03 #Javascript
学习从实践开始之jQuery插件开发 菜单插件开发
May 03 #Javascript
Firefox中beforeunload事件的实现缺陷浅析
May 03 #Javascript
统计jQuery中各字符串出现次数的工具
May 03 #Javascript
JQuery插件Style定制化方法的分析与比较
May 03 #Javascript
拉动滚动条加载数据的jquery代码
May 03 #Javascript
基于jquery的固定表头和列头的代码
May 03 #Javascript
You might like
PHP实现用户认证及管理完全源码
2007/03/11 PHP
PHP 中检查或过滤IP地址的实现代码
2011/11/27 PHP
PHP使用静态方法的几个注意事项
2014/09/16 PHP
CodeIgniter实现从网站抓取图片并自动下载到文件夹里的方法
2015/06/17 PHP
适合PHP初学者阅读的4本经典书籍
2016/09/23 PHP
php 实现Hash表功能实例详解
2016/11/29 PHP
RR vs IO BO3 第二场2.13
2021/03/10 DOTA
javascript 限制输入和粘贴(IE,firefox测试通过)
2008/11/14 Javascript
JavaScript 判断浏览器类型及版本
2009/02/21 Javascript
javascript 文档的编码问题解决
2009/03/01 Javascript
js实现的真正的iframe高度自适应(兼容IE,FF,Opera)
2010/03/07 Javascript
JS input文本框禁用右键和复制粘贴功能的代码
2010/04/15 Javascript
判断浏览器的javascript版本的代码
2010/09/03 Javascript
ASP.NET jQuery 实例7 通过jQuery来获取DropDownList的Text/Value属性值
2012/02/03 Javascript
js中直接声明一个对象的方法
2014/08/10 Javascript
JavaScript中的对象与JSON
2015/07/03 Javascript
分享一些常用的jQuery动画事件和动画函数
2015/11/27 Javascript
简单谈谈javascript中this的隐式绑定
2016/02/22 Javascript
BootStrap响应式导航条实例介绍
2016/05/06 Javascript
node.js从数据库获取数据
2016/05/08 Javascript
通过jquery-ui中的sortable来实现拖拽排序的简单实例
2016/05/24 Javascript
js实现的页面加载完毕之前loading提示效果完整示例【附demo源码下载】
2016/08/02 Javascript
Vue通过URL传参如何控制全局console.log的开关详解
2017/12/07 Javascript
用vue快速开发app的脚手架工具
2018/06/11 Javascript
微信小程序登录态和检验注册过没的app.js写法
2019/05/22 Javascript
Vue axios获取token临时令牌封装案例
2020/09/11 Javascript
使用TS来编写express服务器的方法步骤
2020/10/29 Javascript
[46:37]LGD vs TNC 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/16 DOTA
TensorFlow高效读取数据的方法示例
2018/02/06 Python
python模拟菜刀反弹shell绕过限制【推荐】
2019/06/25 Python
Django 解决model 反向引用中的related_name问题
2020/05/19 Python
汽车检测与维修个人求职信
2013/09/24 职场文书
如何写求职信
2014/05/24 职场文书
学雷锋标语
2014/06/25 职场文书
学习优秀党员杨宗兴先进事迹材料思想汇报
2014/09/14 职场文书
习总书记三严三实学习心得体会
2014/10/13 职场文书