Monday 4 February 2008

More Web Controls - jQuery

Ok so I have an obsession with web controls and since my last one, Yahoo! Media Player, I have been toying with the idea of doing one for John Resig's jQuery. Tonight I got around to doing this and am now releasing version 1 of it. I made the web control as often when I write Web User Controls that contain / use jQuery to enhance the controls I'm not sure if jQuery exists in my web.master or web page itself. This often leads to me having to go back and change pages or include it in the web control only to find one page already uses it. And yes I'm rubbish at remember what JS is used where :S. So this web control not only checks to see if the jQuery control has been rendered before, if not render it but also allows you to simply specify the jQuery version, path to the file and which type to use, normal, minified or packed. Download jQueryWebControl.zip

How To Use It

So add a register assembly into the top of your aspx file
<%@ Register Assembly="jQueryWebControl" Namespace="jQueryWebControl" TagPrefix="jQuery" %>
Then which ever page, master page or even web control will need jQuery add the jQuery Web Control.
<jQuery:jQuery runat="server" ID="jQuery"/>
As the control checks to see if a jQuery control has rendered before you don't have to worry about only including the control once. This should ensure you don't have to waste time checking but also means your controls can be used independantly in different sites.

Using the three optional parameters, Version, Type and Path.

Version - this is just a string parameter that allows you to specify the version of jQuery to use. E.G "1.2.2" - if this is left out then the control defaults to 1.2.0 Type - again a string parameter, this time with three options, empty string "" = default / development version, "min" = minified version, "pack" - the packed version for people who cant serve up gzipped files. - if left the control defaults to the normal development version Path - a string parameter indicating the path to the javascript files. E.G "/javascript/jquery". - if left the control defaults to the /javascript/ It is worth noting that the control expects you to host the jQuery files, so in order to render the script tag for version 1.2.2 pack'd jQuery file you will need jquery.1.2.2.pack.js in the location you specify with the path parameter.

Example of use using parameters

<jQuery:jQuery runat="server" ID="jQuery" Version="1.2.2" Type="min" Path="/javascript/jQuery/"/>
I personally have made use of the version parameter the most as it has means different sites that have been developed against different jQuery releases can be updated / forced to use one version of the library. It also works best if all your jQuery files are in one centralised location, using a virtual directory or a shared location. So hope you like the control, let me know any thoughts, comments or improvements and enjoy. Download jQueryWebControl.zip

2 comments:

Thom said...

How about a universal javascript control? You include it as much as you want in content pages and user controls, just add a src attribute and it'll make sure the file is included only once. Then you can use it with any js libs.

trungIT said...

error in the statement: this.pos=E.easing[this.options.easing||(E.easing.swing?"swing":"linear")](this.state,n,0,1,this.options.duration);

why do the jquery-1.2.2.pack.js file have the error ?
please help me by trungthanhhue@gmail.com
thanks