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 相关文章推荐
HTML上传控件取消选择
Mar 06 Javascript
js图片卷帘门导航菜单特效代码分享
Sep 10 Javascript
基于jQuery实现简单的折叠菜单效果
Nov 23 Javascript
Javascript iframe交互并兼容各种浏览器的解决方法
Jul 12 Javascript
AngularJS表单验证中级篇(3)
Sep 28 Javascript
手机浏览器 后退按钮强制刷新页面方法总结
Oct 09 Javascript
设置jquery UI 控件的大小方法
Dec 12 Javascript
jQuery Validation Engine验证控件调用外部函数验证的方法
Jan 18 Javascript
Vue单页面应用保证F5强刷不清空数据的解决方案
Jan 31 Javascript
JS脚本加载后执行相应回调函数的操作方法
Feb 28 Javascript
vue获取元素宽、高、距离左边距离,右,上距离等还有XY坐标轴的方法
Sep 05 Javascript
vue项目部署到nginx/tomcat服务器的实现
Aug 26 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中mysql连接和基本操作代码(快速测试使用,简单方便)
2014/04/25 PHP
php将文件夹打包成zip文件的简单实现方法
2016/10/04 PHP
PHP如何实现订单的延时处理详解
2017/12/30 PHP
JQery 渐变图片导航效果代码 漂亮
2010/01/01 Javascript
浅析Cookie中的Path与domain
2013/12/18 Javascript
js中style.display=&quot;&quot;无效的解决方法
2014/10/30 Javascript
jQuery实现的网页竖向菜单效果代码
2015/08/26 Javascript
给before和after伪元素设置js效果的方法
2015/12/04 Javascript
理解JS绑定事件
2016/01/19 Javascript
JS原生带小白点轮播图实例讲解
2017/07/22 Javascript
ztree实现左边动态生成树右边为内容详情功能
2017/11/03 Javascript
详细分析JS函数去抖和节流
2017/12/05 Javascript
JS实现验证码倒计时的注册页面
2018/01/02 Javascript
webpack开发环境和生产环境的深入理解
2018/11/08 Javascript
0基础学习前端开发的一些建议
2020/07/14 Javascript
js实现筛选功能
2020/11/24 Javascript
python回溯法实现数组全排列输出实例分析
2015/03/17 Python
Python网络编程使用select实现socket全双工异步通信功能示例
2018/04/09 Python
在Python中Dataframe通过print输出多行时显示省略号的实例
2018/12/22 Python
Python爬虫实现验证码登录代码实例
2019/05/10 Python
Python Numpy计算各类距离的方法
2019/07/05 Python
python文件读写代码实例
2019/10/21 Python
Python调用.net动态库实现过程解析
2020/06/05 Python
django数据模型中null和blank的区别说明
2020/09/02 Python
PyCharm中关于安装第三方包的三个建议
2020/09/17 Python
黄色火烈鸟:De Gele Flamingo
2019/03/18 全球购物
设计师个人求职信范文
2014/02/02 职场文书
机电专业大学生职业规划书范文
2014/02/25 职场文书
广告创意求职信
2014/03/17 职场文书
幼儿园教师的考核评语
2014/04/18 职场文书
商务英语邮件开头问候语
2015/11/10 职场文书
企业管理制度设计时要注意的几种“常见病”!
2019/04/19 职场文书
SQLServer2008提示评估期已过解决方案
2021/04/12 SQL Server
分析设计模式之模板方法Java实现
2021/06/23 Java/Android
 分享一个Python 遇到数据库超好用的模块
2022/04/06 Python
vue打包时去掉所有的console.log
2022/04/10 Vue.js