12. Seeing the Error
• Since theenduser really cannottakeanyactionto fixtheJavaScriptcoming as partofa
webpage,thebrowsereats theerrors.
• As developers, weneed tolook forthe errors -sometimes it takesa minute toeven
remember tocheck fora JSerror.
13.
14. ConsoleLogging
• Debuggingusingalert() can get tiring- sometimes youwantto recordwhathappensin
casesomething goes wrong
• console.log("String") - andmanymorefunctions
Note: Requires recent browsers
20. Comments in JavaScript = Awesome
// This is a comment
/* This is a section of
multiline comments that will
not be interpreted */
21. Statements
• White spaceandnewlines donot matter.
• Statementsendwith a semicolon ;
• There arecases whereyoucan leave the semicolon off,butdon’tbotherexploiting this
feature- just addsemicolons like inC,Java, PHP,C++,etc.
34. Functions
• Functions use atypicalsyntaxandare indicatedusingthe functionkeyword.
• The returnkeywordfunctions asexpected.
<script type="text/javascript">
function product(a,b) {
value = a + b;
return value;
}
console.log("Prod = "+product(4,5));
</script>
35. Scope - Global (default)
• Variablesdefinedoutsidea functionthatare referenced inside of afunctionhave
globalscope.
• This isa littledifferentthanwhatweexpect.
<script type="text/javascript">
gl = 123;
function check() {
gl = 456;
}
check();
window.console && console.log("GL = "+gl);
</script>
36. Making a Variable Local
Ina function,theparameters(formalarguments) arelocalandanyvariables we markwith
thevarkeywordarelocal too.
<script type="text/javascript">
gl = 123;
function check() {
var gl = 456;
}
check();
window.console && console.log("GL = "+gl);
</script>
45. Web Server Database ServerTime
Apache
PHP
MySql
Browser
JavaScri
pt
D
O
M Parse
Respons
e
ind.php P
D
O
Send
Request
http://www.wa4e.com/code/rrc/
46. Document Object Model
• JavaScriptcan manipulatethe current HTML document.
• The “Document Object Model” tells us the syntaxtouse toaccess various “bits”of
the current screen toreadand/ormanipulate.
• You caneven findpieces ofthe model byidattributeandchangethem.
• Wecan read andwritetheDOM fromJavaScript.
http://en.wikipedia.org/wiki/Document_Object_Model
47. DOMs are Not Identical
• Not allbrowsers represent theirpageexactly thesame way.
• This makes it achallenge to keepallofyourJavaScript workingonallbrowsers.
• Italso means youneed totest yourcodeover andover on each browser.
• Aargh..
48.
49. Using getElementById()
Inordernotto havetotraverse each uniqueDOM, weuse afunctioncall thatallbrowsers
support.This allows us tobypassthe DOM structureandjumpto aparticulartagwithin the
DOM andmanipulatethattag.
52. Updating the Browser Document
<a href="#"
onclick="document.getElementById('stuff').innerHTML='BACK';return false;">
Back</a>
<a href="#"
onclick="document.getElementById('stuff').innerHTML='FORTH';return false;">
Forth</a>
</p>
<p>
Hello <b><span id="stuff">Stuff</span></b> there.
This is one reason whyyoucan only have
one id per document.
53. <p>
<a href="#" onclick="add();return false;">More</a>
</p>
<ul id="the-list">
<li>First Item</li>
</ul>
<script>
counter = 1;
console.log(document.getElementById('the-list'))
function add() {
var x = document.createElement('li');
x.className = "list-item";
x.innerHTML = "The counter is "+counter;
document.getElementById('the-list').appendChild(x);
counter++;
}
</script>
js-15.htm
54. JQuery tothe Rescue
• While the DOMs arenotparticularlyportable, and direct DOM manipulationis alittleclunky,
therearea number of JavaScriptframeworks thathandle themyriad of subtlediffereces
between browsers.
• WithJQuery, insteadof manipulatingtheDOM, we useJQuery functions andeverything works
much better...
http://jquery.org
56. Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance
(www.dr-chuck.com) as part of www.wa4e.com and made
available under a Creative Commons Attribution 4.0 License.
Please maintain this last slide in all copies of the document to
comply with the attribution requirements of the license. If you
make a change, feel free to add your name and organization
to the list of contributors on this page as you republish the
materials.
Initial Development: Charles Severance, University of
Michigan School of Information
Insert new Contributors and Translators here including names
and dates
ContinuenewContributorsandTranslatorshere
Editor's Notes
https://www.destroyallsoftware.com/talks/wat
Note from Chuck. Please retain and maintain this page as you remix and republish these materials. Please add any of your own improvements or contributions.