By the time we're done, you'll be able to:
<script type="text/javascript"> document.write('Hello World!
\n'); </script> <body onload="alert('now loaded!');"> <input type="button" value="Click Me" onclick="alert('--Button Click--');" >
<script type="text/javascript" src="myjsfile.js" ></script>
javascript:
protocol:
Click MeDemo
type="text/javascript";
it's assumed.wp_enqueue_script()
action,
and the associated admin_enqueue_scripts()
for scripts on the admin side,
and login_enqueue_scripts()
for scripts on login screens.// Single line comment. /* Multi line comment. See? Another commented line. * /
a
-z
, A
-Z
, $
, _
)0
-9
)var x; y = 3; var z = 'This is a string', q = "This is also a string.";
var
to declare a variable (like y
above), it is created with global scope. Best to always use var
.delete
statement.null
Object
sArray
sNumber
String
Boolean
var name = "\t Rasmus Eich\t\t "; var upperName = name.trim().toUpperCase(); // RASMUS EICHDemo
String
object, performs the toUpperCase()
method, and discards the object.Input | Result |
---|---|
undefined |
NaN |
null |
0 (except IE which returns NaN ) |
Boolean |
true returns 1 ; otherwise 0 (except IE which returns NaN ) |
Number |
No conversion; returns the value |
String |
Integer or float |
Object |
NaN |
+
.\t
, \n
, \\
, \/
, \'
and \"
.\u
and four hexadecimal characters.var mySum1 = "5" + 3 + 8; // "538" var mySum2 = 5 + 3 + "8"; // "88"
var myVal1 = "30" - 10; // 20 var myVal2 = 20 / "2"; // 10 var myVal3 = "10" * 3; // 30
true
or false
."true"
or "false"
.!
.A && B
: if A
is false
, it doesn't bother evaluating B
A || B
: if A
is true
, it doesn't bother evaluating B
&&
,
but only if the value on the left is truthy.||
will return the element on the right, if the element on the left is falsy.Input | Result |
---|---|
undefined |
false |
null |
false |
Boolean |
Value of value |
Number |
false if 0 or NaN ; otherwise, true |
String |
false if empty; otherwise, true |
Object |
true |
null
and undefinednull
is a value you can assign to a variable.0
, but both are falsy.
var a = null; if (a == null) alert('a is null'); var b; if (typeof(b) == 'undefined') alert('b is undefined');
// declaring object with a literal var myObject = { computer: 'MacBook Pro', os: 'OSX', age: 3, "screen-shot": '2012-09-30 screenshot.png' }; // referencing the object console.log(myObject.computer); console.log(myObject['screen-shot']);
var myObject = { computer: 'MacBook Pro', os: 'OSX', age: 3, software: { title: 'Firefox', publisher: 'Mozilla', version: 15.0 } }; console.log(myObject); console.log(myObject && myObject.age); console.log(myObject && myObject.software.title);Demo
var arr1 = new Array(10); var arr2 = new Array(1, 2, 3, 4, 5, 6); var arr3 = [0, 1, 'I am a string, I am.', function() {alert('the media');}]; arr3.prop1 = 'I am a proper property.'; var num = arr2[3];
JavaScript has pretty much the same control structures as PHP:
if
/else
for
while
break
continue
for
/in
(instead of foreach
)There are more, but these will suffice for our purposes.
var arr = [1, 2, 3, 4 ,5]; arr.prop1 = 'Yes, properties in arrays.'; for (var prop in arr) { console.log('prop: ' + prop + ' - value: ' + arr[prop]); }Demo
function doAdd1(a, b) { return a + b; }; var doAdd2 = function(a, b) { return a + b; }; var myObj = { doAdd: function(a, b) { return a + b; } }
call()
and apply()
methodsthis
keyword.this
; but it's either the global object, or undefined.this
refers to the object the method was invoked on.var a = new myFunc();
), this
refers to the newly created object.call()
and apply()
methods, you get to specify the context yourself. But we won't be getting into these.document
Objectdocument
object, which encapsulates the DOM.document.getElementById()
returns an element based on its id.innerHTML
property can be used to update content.document.write()
:
document.write('Outputs to page content');Demo
COMING SOON
COMING SOON
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"> <script src="http://code.jquery.com/jquery-latest.js"> <script>window.jQuery || document.write('<script src="/path/to/local/copy/jquery-1.8.2.min.js"><\/script>')
A couple of interesting side notes:
http
in Google's CDN URL: it'll work with both http and https requests.$
vs. jQuery
$
and jQuery
as its main interface. Everything jQuery does, is done through those two, equivalent, variables.$
. So we have the .noConflict()
utility function to relinquish control of $
:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"> <script> $.noConflict(); (function($) { /* My jQuery code, using $. */ })(jQuery); <script src="/path/to/Prototype.js">
$
in the global namespace, we can continue using it in a function scope.$
for our jQuery code; but only within the lambda.$
Buys YoujQuery packs a LOT of functionality into one variable. Here's what you get:
$.trim(myString)
)$(document).ready(function() { // my JS code... }); $(function() { // my JS code... });
Selector | Results |
---|---|
$('#myID') |
Selects element with ID myID . |
$('.myClass') |
Selects elements with class myClass . |
$('p:odd') |
Selects all odd p elements. |
$('tr:nth-child(3)') |
Selects the third row of each table. |
$('body > div') |
Selects direct div children of body . |
$("a[href$='pdf']") |
Selects links to PDF files. |
$('div:has(a)') |
Selects div s containing links. |
.html()
allows you to get and set HTML within the wrapped set's elements..css()
allows you to get and set the wrapped set's elements' CSS values..addClass()
and .removeClass()
add and remove classes from the wrapped set's elements.
$('p:odd') .html('This is an odd paragraph.') .addClass('odd') .css('color', 'green');Demo
blur() | focusin() | mousedown() | mouseup() |
change() | focusout() | mouseenter() | resize() |
click() | keydown() | mouseleave() | scroll() |
dblclick() | keypress() | mousemove() | select() |
error() | keyup() | mouseout() | submit() |
focus() | load() | mouseover() | unload() |
$.load()
, $.get()
, $.post()
and $.ajax()
. They all use $.ajax()
. We're going to look at $.get()
.
Demo
show()
, hide()
, toggle()
slideUp()
, slideDown()
, slideToggle()
fadeIn()
, fadeOut()
, fadeTo()
animate()