jQuery Tips and Tricks

Add to Favourites
Post to:

© IT Funda Corporation (http://www.itfundacorporation.com/) Visit http://www.dotnetfunda.com for .NET Articles, Tutorials, Forums, Career Advice & more Visit http://www.itfunda.com for .NET online training and training materials Author: Sheo Narayan What is jQuery? jQuery is not a language but it is a well written JavaScript code, As quoted on official jQuery website “it is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development.“ In order to work with jQuery you should be aware of basics of JavaScript, HTML and CSS. It was released in January 2006 at BarCamp NYC by John Resig. Licensing: It is free, open source software Dual-licensed under the MIT License and the GNU General Public License. Microsoft has integrated jQuery officially into its IDE Visual Studio 2010 and jQuery intellisense is available in Visual Studio 2010 now. Why jQuery? jQuery is very compact and well written JavaScript code that increases the productivity of the developer by enabling them to achieve critical UI functionality by writing very less amount of code. It helps to  Improve the performance of the application  Develop most browser compatible web page  Implement UI related critical functionality without writing hundreds of lines of codes  Fast  Extensible – jQuery can be extended to implement customized behavior Other advantages of jQuery are  No need to learn fresh new syntaxes to use jQuery, knowing simple JavaScript syntax is enough  Simple and Cleaner code, no need to write several lines of codes to achieve complex functionality. Where to download jQuery from? jQuery javascript file can be downloaded from jQuery Official website http://www.jquery.com/How to use jQuery? jQuery usually comes as a single JavaScript file containing everything comes out of the box with jQuery. It can be included within a web page using the following mark-up: © IT Funda Corporation (http://www.itfundacorporation.com/) Visit http://www.dotnetfunda.com for .NET Articles, Tutorials, Forums, Career Advice & more Visit http://www.itfunda.com for .NET online training and training materials Author: Sheo Narayan To load local jQuery file Ideally this markup is kept in under tag of your web page, however you are free to keep anywhere you want. Do I need to refer jQuery file both in Master page/base page/template page and content page? No, master page/base page/template page basically helps to create consistent layout for the page in the application. In case you have referred the jQuery file in master page/base page/template page that cause rendering the file in the browser, you do not need to refer jQuery file the content page again. In summary, there should not be more than one Google -To load jQuery from Google Libraries API jQuery file can be loaded from Google CDN for more details, go to http://code.google.com/apis/libraries/devguide.html. You will need to keep following tag in your page. Why to load jQuery file from CDN? You may ask that if we can load the jQuery file from our own server why to load it from the CDNs. The answer is logical and very simple. The browser behavior is that whenever it loads any webpage, it keeps related files (eg. Javascript file, CSS file and Images) used for that page into its cache (also called history). When next time the user browses any web page, browser loads only those files that are new or modified and is not available in the browser cache or history. In this way, browser improves its performance and loads the page. The possibility is that if more and more websites are using CDNs, the user might have already browsed some other web pages that is using CDNs jQuery file and that file may have into browser cache; so when user browse your page and you are also using CDNs file, the older cached version of jQuery file will be used. In this way your page will load faster as browser will not have to load the jQuery file for your page again. The benefit 1. Faster page load as jQuery file need not to be downloaded 2. Saves your bandwidth as jQuery file is not loaded from your server 3. Scalable -generally CDNs place these files on the servers located at different geographical locations of the world so that they load faster so irrespective of from where your user is browsing your page, your application runs well. What if the latest jQuery version is available and I am still referring older version of jQuery file from CDNs? © IT Funda Corporation (http://www.itfundacorporation.com/) Visit http://www.dotnetfunda.com for .NET Articles, Tutorials, Forums, Career Advice & more Visit http://www.itfunda.com for .NET online training and training materials Author: Sheo Narayan Do not worry about it, it’s a general promise made by CDNs that they will remain hosting the older version of the files on the same location where they had initially released; so even if newer version of the files are released, the older version remains there on the CDNs and your web page still works. How to load local jQuery file in case CDN is not available? Sometimes, it may happen that the CDN you have used to load the jQuery file is not available (it rarely happens, however anything is possible, isn’t it?); in that case you should load your local jQuery file that is available on your server so that all jQuery related functionality still work on your page. Write following lines of code Replace bolded path with your own jQuery file path on the server. In the above code, first line tries to load the jQuery file from CDN, if browser could load the file successfully, “jQuery” variable will not be undefined and next script will not run otherwise next script will run that will write the script tag to load the jQuery file from your server. How to execute jQuery code? There are two ways you may want to execute jQuery codes. 1. As and when page loads, execute the jQuery code OR © IT Funda Corporation (http://www.itfundacorporation.com/) Visit http://www.dotnetfunda.com for .NET Articles, Tutorials, Forums, Career Advice & more Visit http://www.itfunda.com for .NET online training and training materials Author: Sheo Narayan The benefit of executing jQuery code in this way is that it doesn’t wait the whole page to load completely, so in case you want user to see the effects as soon as the corresponding elements are loaded, you can use this. However the disadvantage is that if the element on which jQuery has to execute has not loaded then it will error out or you will not get desired result; so while using this way of executing jQuery code, you will have to make sure that the element on which you want to work with jQuery is loaded first (you can place your jQuery code right after your HTML element). 2. Execute jQuery only when the complete DOM objects (the complete page has been loaded). You will have to wrap your code in .ready function. This is the better and safer way to execute jQuery. This makes sure that jQuery code will execute only if complete page has been loaded in the browser so you are rest assured that user will not see any undesired behavior on the page. As a developer, the decision of where and how to write jQuery code lies on you. I prefer to use 2nd method as it ensures that my complete page is loaded in the browser and I am ready to play with any element available on the page. jQuery Features jQuery Core Demo url: http://localhost:9007/jQueryWeb/jQueryCore.aspx jQuery accepts a string enclosed with double quote (“”) that can contain a CSS selector which is used to match a set of elements on the web page. jQuery code can start with either “jQuery” word or a “$” (dollar symbol). Take a look at below code snippet OR © IT Funda Corporation (http://www.itfundacorporation.com/) Visit http://www.dotnetfunda.com for .NET Articles, Tutorials, Forums, Career Advice & more Visit http://www.itfunda.com for .NET online training and training materials Author: Sheo Narayan Both above code snippets are functionally same and do the same thing. So you can either user jQuery or $ when you are writing jQuery code.

Description
This jQuery how to Tips and Tricks ebook and video tutorials covers more than 101+ jQuery methods and written in easy to understand manner. All How to Tips and Tricks are supported by a demo page & source code. This book comes with easy to navigate source code.

Comments

Want to learn?

Sign up and browse through relevant courses.

Name:
Your Email:
Password:
Country:
Contact no:


Area code Number
Subjects you are interested in:
Word verification: (Enter the text as in image)


Sign Up Already a member? Sign In
I agree to WizIQ's User Agreement & Privacy Policy
IT Funda
Learning solution for .NET professionals
User
1 Follower

Your Facebook Friends on WizIQ

Give live classes, create & sell online courses

Try it free Plans & Pricing

Connect