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 相关文章推荐
为超链接加上disabled后的故事
Dec 10 Javascript
jsvascript图像处理—(计算机视觉应用)图像金字塔
Jan 15 Javascript
用js实现trim()的解决办法
Apr 16 Javascript
IE中鼠标经过option触发mouseout的解决方法
Jan 29 Javascript
基于jQuery实现交互体验社会化分享代码附源码下载
Jan 04 Javascript
JS实现微信弹出搜索框 多条件查询功能
Dec 13 Javascript
Vue学习笔记进阶篇之过渡状态详解
Jul 14 Javascript
vue组件初学_弹射小球(实例讲解)
Sep 06 Javascript
使用vue与jquery实时监听用户输入状态的操作代码
Sep 19 jQuery
使用JQuery实现图片轮播效果的实例(推荐)
Oct 24 jQuery
微信小程序获取手机号授权用户登录功能
Nov 09 Javascript
js数组常用最重要的方法
Feb 04 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加密算法之实现可逆加密算法和解密分享
2014/01/21 PHP
php实现中文字符截取防乱码方法汇总
2015/04/29 PHP
Yii核心验证器api详解
2016/11/23 PHP
php用xpath解析html的代码实例讲解
2019/02/14 PHP
PHP中md5()函数的用法讲解
2019/03/30 PHP
关于laravel 子查询 &amp; join的使用
2019/10/16 PHP
JS事件在IE与FF中的区别详细解析
2013/11/20 Javascript
js检测网络是否具体连接功能的代码
2014/05/23 Javascript
javascript常见数据验证插件大全
2015/08/03 Javascript
jquery实现全选、反选、获得所有选中的checkbox
2020/09/13 Javascript
分享15个大家都熟知的jquery小技巧
2015/12/02 Javascript
javascript如何定义对象数组
2016/06/07 Javascript
javascript验证香港身份证的格式或真实性
2017/02/07 Javascript
vue-axios使用详解
2017/05/10 Javascript
JS获取url参数,JS发送json格式的POST请求方法
2018/03/29 Javascript
vue移动端监听滚动条高度的实现方法
2018/09/03 Javascript
vue单页面应用打开新窗口显示跳转页面的实例
2018/09/21 Javascript
使用angular-cli webpack创建多个包的方法
2018/10/16 Javascript
使用异步组件优化Vue应用程序的性能
2019/04/28 Javascript
vue 设置 input 为不可以编辑的实现方法
2019/09/19 Javascript
Js代码中的span拼接问题解决
2019/11/22 Javascript
JavaScript this使用方法图解
2020/02/04 Javascript
VueCli4项目配置反向代理proxy的方法步骤
2020/05/17 Javascript
解决vue字符串换行问题(绝对管用)
2020/08/06 Javascript
解决iview table组件里的 固定列 表格不自适应的问题
2020/11/13 Javascript
Python 变量类型及命名规则介绍
2013/06/08 Python
Python通过属性手段实现只允许调用一次的示例讲解
2018/04/21 Python
浅谈Django前端后端值传递问题
2020/07/15 Python
eVitamins日本:在线购买折扣维生素、补品和草药
2019/04/04 全球购物
高级销售员求职信
2013/10/25 职场文书
护士岗位求职应聘自荐书范文
2014/02/12 职场文书
个人委托书范本汇总
2014/10/01 职场文书
教师党的群众路线教育实践活动个人整改方案
2014/10/31 职场文书
Mysql binlog日志文件过大的解决
2021/10/05 MySQL
Python面向对象编程之类的概念
2021/11/01 Python
Redis特殊数据类型HyperLogLog基数统计算法讲解
2022/06/01 Redis