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 相关文章推荐
[原创]图片分页查看
Aug 28 Javascript
jQuery下实现等待指定元素加载完毕(可改成纯js版)
Jul 11 Javascript
瀑布流布局代码一例
Apr 11 Javascript
基于Jquery实现键盘按键监听
May 11 Javascript
获取JavaScript异步函数的返回值
Dec 21 Javascript
angular实现form验证实例代码
Jan 17 Javascript
JS实现JSON.stringify的实例代码讲解
Feb 07 Javascript
一篇文章让你彻底弄懂JS的事件冒泡和事件捕获
Aug 14 Javascript
seajs中最常用的7个功能、配置示例
Oct 10 Javascript
浅谈vuex 闲置状态重置方案
Jan 04 Javascript
vue生命周期实例小结
Aug 15 Javascript
微信小程序实现登录遮罩效果
Nov 01 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代码
2006/12/06 PHP
THINKPHP+JS实现缩放图片式截图的实现
2010/03/07 PHP
php自动加载的两种实现方法
2010/06/21 PHP
探讨fckeditor在Php中的配置详解
2013/06/08 PHP
从PHP $_SERVER相关参数判断是否支持Rewrite模块
2013/09/26 PHP
CI框架开发新浪微博登录接口源码完整版
2014/05/28 PHP
54个提高PHP程序运行效率的方法
2015/07/19 PHP
php+js实现的无刷新下载文件功能示例
2019/08/23 PHP
Laravel监听数据库访问,打印SQL的例子
2019/10/24 PHP
JavaScript 私有成员分析
2009/01/13 Javascript
页面版文本框智能提示JS代码
2009/11/20 Javascript
jQuery操作 input type=checkbox的实现代码
2012/06/14 Javascript
js 弹出框只弹一次(二次修改之后的)
2013/11/26 Javascript
JavaScript支持的最大递归调用次数分析
2014/06/24 Javascript
Node.js中安全调用系统命令的方法(避免注入安全漏洞)
2014/12/05 Javascript
JS排序之选择排序详解
2017/04/08 Javascript
微信小程序--onShareAppMessage分享参数用处(页面分享)
2017/04/18 Javascript
js禁止Backspace键使浏览器后退的实现方法
2017/09/01 Javascript
基于Vue组件化的日期联动选择器功能的实现代码
2018/11/30 Javascript
echarts大屏字体自适应的方法步骤
2019/07/12 Javascript
jQuery实现王者荣耀手风琴效果
2020/01/17 jQuery
vue打包通过image-webpack-loader插件对图片压缩优化操作
2020/11/12 Javascript
Python基于pygame实现的弹力球效果(附源码)
2015/11/11 Python
python中string模块各属性以及函数的用法介绍
2016/05/30 Python
python实现BackPropagation算法
2017/12/14 Python
tensorflow 实现数据类型转换
2020/02/17 Python
如何理解python对象
2020/06/21 Python
Idea安装python显示无SDK问题解决方案
2020/08/12 Python
Django如何重置migration的几种情景
2021/02/24 Python
CSS3实现文本垂直排列的方法
2018/07/10 HTML / CSS
管理学专业个人求职信范文
2013/12/13 职场文书
四年级科学教学反思
2014/02/10 职场文书
2014年会策划方案
2014/05/11 职场文书
大学生暑期社会实践证明范本
2014/10/24 职场文书
鲁迅故居导游词
2015/02/05 职场文书
Java使用HttpClient实现文件下载
2022/08/14 Java/Android