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基于HTML5 canvas制作画箭头组件
Jun 25 Javascript
jQuery中eq()方法用法实例
Jan 05 Javascript
jQuery中wrapAll()方法用法实例
Jan 16 Javascript
举例详解AngularJS中ngShow和ngHide的使用方法
Jun 19 Javascript
不依赖Flash和任何JS库实现文本复制与剪切附源码下载
Oct 09 Javascript
Jquery Mobile 自定义按钮图标
Nov 18 Javascript
jQuery ajax时间差导致的变量赋值问题分析
Jan 22 Javascript
jQuery动态追加页面数据以及事件委托详解
May 06 jQuery
webpack构建的详细流程探底
Jan 08 Javascript
Node.js使用Angular简单示例
May 11 Javascript
layui实现显示数据表格、搜索和修改功能示例
Jun 03 Javascript
Vue向后台传数组数据,springboot接收vue传的数组数据实例
Nov 12 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
Adodb的十个实例(清晰版)
2006/12/31 PHP
windows服务器中检测PHP SSL是否开启以及开启SSL的方法
2014/04/25 PHP
微信access_token的获取开发示例
2015/04/16 PHP
php递归函数三种实现方法及如何实现数字累加
2015/08/07 PHP
php实现微信模板消息推送
2018/03/30 PHP
javascript的trim,ltrim,rtrim自定义函数
2008/09/21 Javascript
html 锁定页面(js遮罩层弹出div效果)
2009/10/27 Javascript
默认让页面的第一个控件选中的javascript代码
2009/12/26 Javascript
优化javascript的执行速度
2010/01/23 Javascript
JS中产生20位随机数以0-9为例也可以是a-z A-Z
2014/08/01 Javascript
用jquery模仿的a的title属性的例子
2014/10/22 Javascript
jQuery源码解读之removeAttr()方法分析
2015/02/20 Javascript
基于JavaScript实现移动端点击图片查看大图点击大图隐藏
2015/11/04 Javascript
基于jQuery实现仿微博发布框字数提示
2016/07/27 Javascript
Jquery Easyui搜索框组件SearchBox使用详解(19)
2016/12/17 Javascript
jQuery中用on绑定事件时需注意的事项
2017/03/19 Javascript
angular4 共享服务在多个组件中数据通信的示例
2018/03/30 Javascript
jQuery实现鼠标移入移出事件切换功能示例
2018/09/06 jQuery
小程序云开发实战小结
2018/10/25 Javascript
jQuery 判断元素是否存在然后按需加载内容的实现代码
2020/01/16 jQuery
Vue强制组件重新渲染的方法讨论
2020/02/03 Javascript
vue prop传值类型检验方式
2020/07/30 Javascript
[16:43]Heroes19_剃刀(完美)
2014/10/31 DOTA
让 python 命令行也可以自动补全
2014/11/30 Python
python文件特定行插入和替换实例详解
2017/07/12 Python
django 实现编写控制登录和访问权限控制的中间件方法
2019/01/15 Python
python使用matplotlib画柱状图、散点图
2019/03/18 Python
Windows10下 python3.7 安装 facenet的教程
2019/09/10 Python
Django框架静态文件处理、中间件、上传文件操作实例详解
2020/02/29 Python
团员的自我评价
2013/12/01 职场文书
2014年秋季开学寄语
2014/08/02 职场文书
2014年物业公司工作总结
2014/11/22 职场文书
2014年音乐教师工作总结
2014/12/03 职场文书
2015年护士工作总结范文
2015/03/31 职场文书
MySQL 8.0 驱动与阿里druid版本兼容问题解决
2021/07/01 MySQL
Vue的列表之渲染,排序,过滤详解
2022/02/24 Vue.js