VC Bestpractice Guide
VC Bestpractice Guide
to web development
and JavaScript
Knowledge
Describe the components of a client-server architecture.
Describe HTTP requests and responses.
Distinguish between the way a web server processes static web
pages and dynamic web pages.
Name the common web browsers, web servers, and server-side
scripting languages.
Describe the use of the core web technologies: XHTML, CSS, the
Document Object Model, and JavaScript.
• But the data was empty, ugly, and formless. So on the next day the
W3C was formed, and Håkon Wium Lie created CSS to separate the
layout from the design and from the semantic markup.
• On the third day, Brendan Eich created JavaScript; breathing life into
the web content. It was given dominion over all of the DOM from
lowly HTML elements, to the CSS properties, and to the browser
window itself.
• And on the last day, the W3C rested… and let Microsoft screw it all
up with Internet Explorer. But because Microsoft blessed us with
the gift of AJAX, they were forgiven.
W3C languages as a Metaphor
If the W3C languages were a person…
• JavaScript runs in the client, that is, the browser. If you use
an older browser without support for JavaScript, or if you
simply choose to disable JavaScript in your browser, then a
JavaScript script can't work.
• No Database access
• No File system access
• No hardware (peripheral) access
• No low level networking (http only)
Zillion things you can do with
JavaScript
Put HTML/Text in a page on-the-fly.
Say you want to display a nice thank you message to a user who has
just submitted a comment form on your website. Obviously, the
message needs to be added after the user has submitted the form.
You could let the server do that. However, if your website is very busy
and your server processes hundreds of forms a day, it might take a
little while for your thank you message to appear to the user.
Web environments are dynamic, things happen all the time: the
web page loads in the browser, the user clicks a button or moves
the mouse over a link, etc. These are called events.
You can use a JavaScript script to detect the visitor’s browser, or,
even better, you can detect what features a certain browser does
or does not support. Depending on the browser and its
capabilities, you can choose to load a page specifically tailored to
that kind of browser.
See: http://modernizr.com/
Zillion things you can do with
JavaScript
Create cookies.
You can use a JavaScript script to validate form data before the
form is submitted to a server. This saves the server from extra
processing, and gives the user real-time feedback.
Zillion things you can do with
JavaScript
And graphics & animation to HTML.
See: http://www.flotcharts.org/
Zillion things you can do with
JavaScript
Talk to the web server asynchronously.
In non-AJAX web applications, the interaction between servers and clients can be a tedious business:
• a user action from the client sends a request to the web server via HyperText Transfer Protocol (HTTP);
• the web server receives the request and processes it by invoking server-side scripts, database data, etc., and sends a
response back to the client via HTTP;
• the client browser receives the response and loads the entire updated page.
Having to go from browser to server and back again each time a user requests some piece of data from the server, in
addition to undergoing an entire page refresh at each update, can be quite stressful on servers and on users alike. AJAX
helps in at least 2 respects: piecemeal page updates and asynchronous communication between server and client.
What this means in a few words, is that every time the user interacts with the web page, only those bits that need updating
are refreshed, not the entire page. Furthermore, the fact that AJAX operations are performed asynchronously, means that
during the time that it takes for the server to respond, the page is not locked and the user can still interact with the website.
What else can JavaScript do?
How much time ya got?
What are the down sides of
JavaScript?
1. Compatibility – different browsers use different JavaScript engines and support
different features. Making it almost required to use frameworks that detect and
neutralize the compatibility issues (allowing you to get back to work).
3. Availability – a user can disable JavaScript at any time, and only they can enable
it again. This makes it incredibly impractical and time consuming to write a “Web
2.0” application that gracefully fails back to a “Web 1.0” standards when
JavaScript is disabled.
4. Security – JavaScript is parsed and interpreted by the client, thus the raw
JavaScript source is viewable to any sophisticated user who wishes to view it. As
such, you can’t fully rely on JavaScript to implement security related features.
This forces you to validate security twice, once on the client and then again on
the server.
So when should I use JavaScript vs.
something else?
• JavaScript is the ONLY language that can directly interact with the user or
browser window. If you need…
– browser inspection
– live DOM manipulation
– event handling
– or interactivity of any kind… look no further.
• If your user experience will suffer from excessive page refreshing and/or
data shuffling, use AJAX. Pulling in small chunks of JSON/XML data from
the server and updating a live DOM via JavaScript can GREATLY increase
the responsiveness of your applications.
HTTP request
XHTML
file
`
HTTP response
<html>
<head>
<title>Example Web Page</title>
</head>
<body>
<p>This is a sample web page</p>
</body>
</html>
Terms
Hypertext Markup Language (HTML)
static web page
HTTP request
HTTP response
HTTP request
`
HTTP response
Application
script
HTTP request
`
HTTP response
Application
JavaScript
script
head body
title h1 p h2 ul p
li li li
Web browser
Web page (XHTML and CSS)
DOM
JavaScript
Page
Loaded
<label for="total">Total:</label>
<input type="text" id="total"
disabled="disabled" /><br />
<label> </label>
<input type="button" id="calculate"
value="Calculate" /><br />
</div>
</div>
</body>
</html>
window.onload = function () {
$("calculate").onclick = calculate_click;
$("subtotal").focus;
}
$("salesTax").value = "";
$("total").value = "";
Note
These skills are presented in figure 2-1.
A multi-line comment
/* The following line determines what the
next year is by adding 1 to the current year
*/
nextYear = thisYear + 1;
Terms
integer
floating-point value
exponent
empty string
escape sequence
Boolean value
(3 + 4) * 5 // Result is 35
13 % 4 + 9 // Result is 10
13 % (4 + 9) // Result is 0
window.location = "http://www.murach.com";
// Loads the murach.com home page
Examples
var balance = 2384.55678;
// creates a number object named balance
alert ( balance.toFixed(2) );
// displays 2384.56
// balance is still 2384.55678
Examples
var guest = "Ray Harris";
// creates a string object named guest
Examples
var today = new Date();
// creates a Date object named today
alert ( today.getMonth() );
// displays 8, not 9, for September
isNaN("123.45")
// Returns false since "123.45"
// can be converted to a number
<label> </label>
<input type="button" id="calculate"
value="Calculate" /><br />
</body>
window.onload = function () {
$("calculate").onclick = calculate_click;
$("investment").focus();
}
// Get the user entries from the first three text boxes.
var investment = parseFloat( $("investment").value );
var annualRate = parseFloat( $("rate").value );
var years = parseInt( $("years").value );
// Set the value of the fourth text box to the future value
$("futureValue").value = futureValue.toFixed(2);
}
}
Description
The equality operators perform type coercion.
The identity operators do not perform type coercion.
The OR operator
state == "CA" || state == "NC"
if ( isNaN(average) ) {
alert("You didn't enter any numbers.");
} else {
alert("The average is: " + average);
}
do {
number = parseFloat( prompt("Enter a number") );
if ( isNaN(number) ) {
stop = true;
} else {
value_entered = true;
if ( number > max ) max = number;
if ( number < min ) min = number;
}
} while ( !stop );
if (value_entered) {
alert("Max: " + max + ", Min: " + min);
} else {
alert("No numbers entered.");
}
if (prime) {
alert( number + " is prime.");
} else {
alert( number + " is not prime.");
}
Knowledge
Distinguish between a named function and an anonymous
function.
Distinguish between passing an argument by value and passing an
argument by reference.
function coin_toss() {
return (Math.random() > 0.5) ? "Heads" : "Tails";
}
window.onload = function () {
$("button_a").onclick = button_a_click;
$("button_b").onclick = button_b_click;
}
Term
In JavaScript, a nested function has access to the outer function’s
variables. This is called closure.
number = parseInt(number);
if ( isNaN(number) ) continue;
$("item_code").focus();
}
invoice.push(item);
update_display();
}
window.onload = function () {
$("item_add").onclick = item_add_click;
$("item_code").focus();
}
Knowledge
Describe how an index is used to access the data that’s stored in
an array.
Describe the length property of an array.
Distinguish between the for statement and the for-in statement.
alert (namesString1);
alert (namesString2);
to_delete = parseInt(to_delete);
if ( isNaN(to_delete) ) {
alert("You did not enter a number.");
return;
}
if ( to_delete < 1 ) {
alert("The task number is too low.");
return;
}
if ( to_delete > task_list.length ) {
alert("The task number is too high.");
return;
}
to_delete--;
task_list.splice(to_delete, 1);
update_task_list();
}
// Using brackets
invoice["taxRate"] = 0.0875; // property
Date object
now
Vehicle.prototype.change_oil = function () {
this.last_oil_change = this.miles;
return this;
}
Vehicle.prototype.need_oil_change = function () {
return ( this.miles - this.last_oil_change > 3000 );
}
Term
A cascading method can be chained with other methods.
var invoice = {
getSalesTax: function( subtotal ) {
return ( subtotal * invoice.taxRate );
},
getTotal: function ( subtotal, salesTax ) {
return subtotal + salesTax;
},
taxRate: 0.0875,
dueDays: 30
}
Term
A for-in statement loops through all the defined properties of an
object that are enumerable.
function Invoice() {
this.items = [];
this.tax_rate = 0.07;
}
var invoice = new Invoice();
item_code = item_code.toUpperCase();
if ( item_code in this.items ) {
delete this.items[item_code];
}
this.items[item_code] = item_info;
this.sort_by_code();
return this;
}
Invoice.prototype.get_item_list = function() {
var item_list, line_cost, item_count = 0;
item_list = "Item Code".pad_right(10) + " ";
item_list += "Item Name".pad_right(40) + " ";
item_list += "Qty ";
item_list += "Item Cost ";
item_list += "Line Cost\n";
item_list += "".pad_right(10,"-") + " ";
item_list += "".pad_right(40,"-") + " ";
item_list += "--- ";
item_list += "".pad_right(9,"-") + " ";
item_list += "".pad_right(9,"-") + "\n";
for ( var code in this.items ) {
Invoice.prototype.get_sales_tax = function () {
var subtotal = this.get_subtotal();
var sales_tax = subtotal * this.tax_rate;
return parseFloat( sales_tax.toFixed(2) );
}
Invoice.prototype.get_total = function () {
var total = this.get_subtotal() + this.get_sales_tax();
return parseFloat( total.toFixed(2) );
}
$("item_code").value = "";
$("item_name").value = "";
$("item_cost").value = "";
$("item_qty").value = "1";
$("item_delete_code").value = "";
$("item_code").focus();
}