WebCampBE Jquery GillCleeren
WebCampBE Jquery GillCleeren
jQuery zero,
go out as jQuery
Gill Cleeren hero
.NET Architect
Microsoft Regional Director
Silverlight MVP
Ordina Belgium
Glad to meet you...
Gill Cleeren
Microsoft Regional Director
Silverlight MVP
.NET Architect @Ordina (www.ordina.be)
Speaker (TechEd, TechDays BE – Sweden - Switzerland,
DevDays NL, NDC Norway, Spring Conference UK, SQL Server
Saturday Switzerland...)
Visug user group lead (www.visug.be)
Author (Silverlight 4 Data and services cookbook)
www.snowball.be - gill@snowball.be - @gillcleeren
What we’ll be looking at...
Why jQuery?
jQuery fundamentals
Creating and manipulating elements
Events
Animations and effects
Talking to the server
jQuery UI After
t
You‘l hese topic
Writing plugins l say.
..
s,
jQuery is
Most popular, cross-browser JavaScript library
Focusing on making client-side scripting of HTML simpler
Easy navigating the DOM
Handling events
Working with Ajax
Open-source, released in 2006
Why jQuery?
More at http://docs.jquery.com/Sites_Using_jQuery
Microsoft and jQuery
Selec
ts
applie all a’s wi
d wit th som
Most basic: CSS selectors hin a
parag eClass
raph
$("p a.someClass")
Also
in clude
Can be combined s all D
IVs o
n the p
$("p a.someClass, div") Selec age
ts
withi all links,
n an U di
L wit rectly in a
Child selector h som n
eList LI,
class
$("ul.someList > li > a") Selec
ts
a refe all links t
rence h
to my at contain
Attribute selector s
site
l l SPAN $("a[href*='http://www.snowball.be']")
ta s Selec
Selec ve a clas $("span[class^='some']") t all S
h a a tt star PA
that ts wit Ns whose
$("span[class]") h som class
e
jQuery fundamentals: selectors
Selec
ts first A
we ca
n find o
n the
Position page
Selec
ts the “e
$("a:first") ven”
Selec D
IVs o
ts the n a pa
name first c ge
d som e
eTabl lls within
$("div:even")
e a tabl
e
$("table#someTable td:first-child")
* any element
E an element of type E
E[foo] an E element with a "foo" attribute
$(function(){
$('<img>', {
src: 'someImage.jpg',
alt: 'Some nice image'
})
.appendTo('body');
DEMO
Creating elements
using $
Working with the result of $
Filter elements
$("a").filter("[href^='http://']");
A bit of history
Once upon a time, a browser called Netscape introduced an
event model, often referred to as DOM Level 0 Event Model
Creates event handlers as references to a function on a property
Not what we need if we want to create Unobtrusive JavaScript
Only one event handler per element for specific event
Only got standardized until DOM Level 2 Event Model
Based on a system of event listeners (addEventListener)
IE decided to go its own way (attachEvent)
Using event was a real mess because of browser
dependencies
jQuery comes to the rescue
jQuery events
$('#someDiv')
.load('test.html', Load
function() { s the H
T ML p
alert('Load was performed.'); age
});
$.ajax({
type: "post",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "/Default.aspx/AddTask",
data: JSON.stringify(dto)
});
DEMO
ASP.NET WebForms
with jQuery
DEMO
ASP.NET MVC with
jQuery
jQuery UI
2 options:
Use an existing plugin
jQuery plugin repository: plugins.jquery.com
Google code: code.google.com
SourceForge: sourceforge.net
GitHub: github.com
Write a plugin yourself
Custom utility function
Create wrapper functions
DEMO
Using a plugin
Writing your own plugins
3 plugins:
.tmpl(): renders the template
.tmplItem(): find the template item
.template(): compile the template
$("#movieTemplate").tmpl(movies)
.appendTo("#movieList");
jQuery Templates (2)
$.format()
Formats date, currencies and numbers
Accepts value and format specifier (D, c...)
$.preferCulture(“nl-BE”)
Sets default culture
$.cultures()
Returns all available cultures
$.parseDate()
Converts string into JavaScript date
DEMO
jQuery Globalization
Where to get your stuff?
Use a CDN?
Microsoft
Google
Put scripts locally as well with a fallback mechanism
<script type="text/javascript"
src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js">
</script>
<script type="text/javascript">
if (typeof jQuery == 'undefined')
{
document.write(unescape("%3Cscript src='/Scripts/jquery-1.4.2.min.js'
type='text/javascript'%3E%3C/script%3E"));
}
</script>
Summary