A video of this class is (or will be) posted at: http://echo360.uml.edu/heines201516/guiprogramming1.html
Long Tran — please see me
Thank you to Madhu for identifying a problem with the JavaScript code that I gave you for Assignment No. 5. When you run my code on a local Windows or Macintosh system, it works fine. But when you run it on weblab.cs.uml.edu (a Linux system), for some reason that I do not understand jQuery.get() returns the JSON data as a string rather as an actual JSON object. I don’t know if this is a difference in operating system defaults or a problem caused by how connections to weblab.cs.uml.edu is configured or something else.
However, I am happy to report that the problem can be solved by adding an additional parameter to the jQuery.get() function call that explicitly tells the server that you want JSON data. That parameter is simply the string "json" (including the quotes). Here is the modified code that you should use:
This code also differs just slightly from what I gave you earlier by eliminating the
global variable story. Instead, it passes the JSON data read from the file directly to
the placeContent function. This approach is slightly more efficient and has the
advantage of eliminating a global variable, which is almost always a good thing to do.
(Thanks to Curran Kelleher for this enhancement.) Note that this new approach
requires that a story parameter be added to the placeContent function.
One last thing: it took me almost two hours to figure out the problem that Madhu identified, but ultimately the answer was in the jQuery documentation. See page:
This isn’t the easiest documentation to understand, but it is actually better that the documentation you will find for other JavaScript libraries.
See the Piazza posts by Curran Kelleher and me under “Why Asynchronous?”
Code posted on StackOverflow at http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects
My version
A rather amazing look at JavaScript
Related reading for this class: JS&jQ:TMM Ch. 1-3
Client-side computing
Basic structure and use
script tag
type="text/javascript" attributemain function, executes in-line with HTMLonLoad property of the BODY tag
javascript: protocol in your browsers address
fieldBackground work to prepare yourself for Assignment No. 6
document.write and document.writeln
window.alert(), especially for debugging
console.log()Variables
var statement no declared types!
str, n or int, arr, plus prefixes for various object types"" to force string typesparseInt to convert a string read from a field
to an integer so it can be used in calculations""Scope
var are global, even if they’re inside of functions
for ( k = 0 ; k < 10 ; k++ ) { ... }for ( var k = 0 ; k < 10 ; k++ ) { ... }Control structures
// or /*...*/Loops
JavaScript and types — http://www.pollev.com
3 + 5 ? "k" + "s" ? "x" + 7 ? "8" + "4" ? "8" + 4 ? 8 + "4" ? 6 == 6 ? 7 == "7" ?7 === "7" ? "" == 0 ?"" != 0 ?"" !== 0 ?function statementnull, sort of ...
null using ==, you get true, but you can’t differentiate that from a truly null parameternull using ===, you get false, which allows you to differentiate unsupplied parameters from truly null parameters
if ( typeof( param ) === "undefined" ) { ... } true for undefined parametersfunction test( a, b, c ) { ... }var test = function( a, b, c ) { ... } arguments object holds all the parameters pass to a function
arguments is “array-like,” but it is not an instance of the Array typearguments.length returns the number of parameters passed to a functionhead sectionreturn statement, but again there is no declared type for the
functionFunction declarations vs. function expressions
function sum_v1( n1, n2 ) {
return n1 + n2 ;
} ;
document.writeln( sum_v1( 1, 2 ) ) ;var sum_v2 = function( n1, n2 ) {
return n1 + n2 ;
} ;
document.writeln( sum_v2( 3, 4 ) );var sum_v2a = function( n1, n2 ) {
return "sum = " + n1 + n2 ;
} ;
document.writeln( sum_v2a( 3, 4 ) ) ;var sum_v2b = function( n1, n2 ) {
return "sum = " + ( n1 + n2 ) ;
} ;
document.writeln( sum_v2b( 3, 4 ) ) ;var sum_v3 = function () {
return "sum = " + ( arguments[0] + arguments[1] ) ;
} ;
document.writeln( sum_v3( 5, 6 ) ) ;var sum_v4 = function () {
var sum = 0 ;
for ( var k = 0 ; k < arguments.length ; k++ ) {
sum += arguments[k] ;
}
return "sum = " + sum ;
} ;
document.writeln( sum_v4( 7, 8, 9 ) ) ;Key concept: FUNCTIONS ARE SIMPLY EXECUTABLE VARIABLES
var sum_v5 = function() {
sum_v5 = function() { return "I'm bad!" } ;
return "I'm good!" ;
} ;
document.writeln( sum_v5() ) ;
document.writeln( sum_v5() ) ;Run all of the above functions
form tag
method attribute: post or getaction attribute: a URL for the program or script or page that will process the forminput tag
button, checkbox, file, hidden, image, password, radio, reset, submit, or text
hidden and image display elements with which the user can interacthidden passes information to the form processor outside the user’s controlimage is really a variant of the submit tag (discussed below)value attribute: the component’s corresponding default text (changed when the user enters new text)select attribute: controls whether the element is initially selected (changed when the user clicks radio buttons, check boxes, etc.)id attribute: the component’s name for referencing via scripts within the current pagename attribute: the component’s name for referencing via scripts in the form processor pageid and name attributes even though they’re both the same
<input type="text" name="lastname" id="lastname" size="40">select tag
option tagssubmit and reset tagsWorking with the location object
http://jesseheines.com/~heines/91.461/91.461-2015-16f/461-lecs/lecture11.jsp?one=1&two=2&three=3
location.searchlocation.search.substr(1)location.search.substr(1).split("&")location.search.substr(1).split("&").lengthlocation.search.substr(1).split("&")[0].split("=")location.search.substr(1).split("&")[1].split("=")location.search.substr(1).split("&")[0].split("=")[0]location.search.substr(1).split("&")[0].split("=")[1]obj = {} ;obj
Object{ }obj[location.search.substr(1).split("&")[0].split("=")[0]] = location.search.substr(1).split("&")[0].split("=")[1]obj
Object{ one = "1" }
Input
http://jesseheines.com/~heines/91.461/91.461-2015-16f/461-lecs/lecture11.jsp?one=1&three=3&two=2
Output