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

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="https://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="https://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

(0)

相关推荐

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

    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, jQu

  • Jquery uploadify 多余的Get请求(404错误)的解决方法

    在使用jquery uploadify时如果不设置button_image_url参数,就会出现一些多余的get请求,甚至报404的错误,这是该插件的一个bug,官方给出的解决方案如下: 找到如下代码: this.settings.upload_url = SWFUpload.completeURL(this.settings.upload_url); this.settings.button_image_url = SWFUpload.completeURL(this.settings.but

  • 总结AngularJS开发者最常犯的十个错误

    前言 AngularJS易于开发.较多的特征及较好的效果导致了较多的应用,伴随而来的是一些陷阱.本文列举了AngularJS的一些共同的易于出问题的地方,下面来一起看看吧. 一.MVC目录结构 AngularJS,直白地说,就是一个MVC框架.它的模型并没有像backbone.js框架那样定义的如此明确,但它的体系结构却恰如其分.当你工作于一个MVC框架时,普遍的做法是根据文件类型对其进行归类: templates/ _login.html _feed.html app/ app.js cont

  • 10种Java开发者编写SQL语句时常见错误

    Java开发者对于面向对象编程思维与命令行编程思维的协调程度,取决于他们如下几种能力的水平: 技巧(任何人都可以编写命令行形式的代码) 教条(有的人使用"模式 - 模式"的方式,即模式无处不在,并以名字作为标识) 情绪状况(在初期,真正面向对象形式的代码比起命令式代码会更加难懂.) 但是,当Java开发人员编写SQL语句时,一切都变得不同了.SQL是一种说明式语言,与面向对象思想和命令式思想无关.在SQL语言中,查询非常容易表达.但它也不是那么容易以最佳或最正确地方式编写出来.开发人员

  • jquery.validate[.unobtrusive]和Bootstrap实现tooltip错误提示问题分析

    类似的文章已有,请看这里,个人感觉稍显复杂,日前也打算写一个简单的给项目用,一些关键点记录于此.最终效果如下: 后端使用Asp.net mvc5,前端框架有:jquery.validate.jquery.validate.unobtrusive.requirejs.Bootstrap,都是当前最/较新版本.jquery.validate就不用说了,目前比较流行的前端校验组件:jquery.validate.unobtrusive基于jquery.validate,是为了配合Asp.net mvc

  • jQuery开发者都需要知道的5个小技巧

    1.禁用右键菜单 复制代码 代码如下: $(document).ready(function(){ $(document).bind("contextmenu",function(e){ return false; }); }); 2.让字体闪烁 复制代码 代码如下: jQuery.fn.flash = function( color, duration ) { var current = this.css( 'color' ); this.animate( { color: 'rgb

  • jQuery 表单验证插件formValidation实现个性化错误提示

    其效果图如下:使用说明 需要使用jQuery库文件和formValidation库文件[下载实例代码] http://jquery.com/ 同时需要自定义显示提示错误信息的CSS样式 使用实例 一,包含文件部分 复制代码 代码如下: <script src="jquery.js" type="text/javascript"></script> <script src="jquery.validationEngine.js&

  • jQuery 出现Cannot read property ‘msie’ of undefined错误的解决方法

    jQuery Cannot read property 'msie' of undefined错误的解决方法 最近把一个项目的jQuery升级到最新版,发现有些页面报如下错误 Cannot read property 'msie' of undefined 上jQuery网站上搜了一下,原因是$.browser这个api从jQuery1.9开始就正式废除,js代码里只要用到$.browser就会报这个错.具体说明参见jQuery官方说明. 楼主顺便扩展阅读了一下,发现jQuery 1.9把所有在

  • jquery.bgiframe.js在IE9下提示INVALID_CHARACTER_ERR错误

    jquery.bgiframe.js在IE9下的错误 复制代码 代码如下: SCRIPT5022: DOM Exception: INVALID_CHARACTER_ERR (5) jquery.bgiframe.js, 行8 字符976 错误代码 复制代码 代码如下: 1 {if(!$('iframe.bgiframe',this)[0])this.insertBefore(document.createElement(html),this.firstChild);});};})(jQuery

  • 解决jQuery使用JSONP时产生的错误

    什么是域,简单来说就是协议+域名或地址+端口,3者只要有任何一个不同就表示不在同一个域.跨域,就是在一个域中访问另一个域的数据. 如果只是加载另一个域的内容,而不需要访问其中的数据的话,跨域是很简单的,比如使用iframe.但如果需要从另一个域加载并使用这些数据的话,就会比较麻烦.为了安全性,浏览器对这种情况有着严格的限制,需要在客户端和服务端同时做一些设置才能实现跨域请求. JSONP简介 JSONP(JSON with Padding)是一种常用的跨域手段,但只支持JS脚本和JSON格式的数

随机推荐