570 likes | 873 Views
Lecture 9. Javascript Toolkits HTML5 Canvas Forms. JavaScript Toolkits. Many client-side scripting tasks can be done using JavaScript libraries / toolkits Save coding many common graphic elements Best-known ones are JQuery and Dojo, both free
E N D
Lecture 9 Javascript Toolkits HTML5 Canvas Forms
JavaScript Toolkits • Many client-side scripting tasks can be done using JavaScript libraries / toolkits • Save coding many common graphic elements • Best-known ones are JQuery and Dojo, both free • Google Web Toolkit contains both a JavaScript library and a Java-to-JavaScript cross-compiler • Code UI in Java using Swing-like code, automatically compile it into JavaScript that can be placed in any web page.
JQuery • Very widely –used open-source JavaScript library • Download the main library from jquery.com, put a copy on your server, and link to it from your documents • There are also several other JQuery libraries • Write functions that use its functions using syntax like this: • $("#item").jqfunction(arguments);
JQuery <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <script src = "jquery-2.0.3.js"> </script> </head> <body onload = "hideImage()"> <p>One Paragraph</p> <p id = "test">Other paragraph</p> <script> function hideImage(){ $("#test").fadeOut(8000); } </script> </body> </html> Example from Jamsa, Intro To Web Development Using HTML5
JQuery This one uses JQueryUI, another JQuery library. On a server, use a copy of the library and your own stylesheet, but this demo uses ones on JQuery's server. <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI Sortable - Default functionality</title> <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <link rel="stylesheet" href="http://www.jqueryui.com/resources/demos/style.css"> <style>
JQuery #sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; } #sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; } #sortable li span { position: absolute; margin-left: -1.3em; } </style>
JQuery <script> $(function() { $("#sortable").sortable(); $("#sortable").disableSelection(); }); </script> </head> <body> <ul id="sortable"> <li class="ui-state-default"> <span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Spock </li> <li class="ui-state-default"> <span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Uhura </li> <li class="ui-state-default"> <span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Kirk </li> </html>
JQuery <li class="ui-state-default"> <span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Sulu </li> <li class="ui-state-default"> <span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Chekhov </li> <li class="ui-state-default"> <span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Chapel </li> </ul> </body> </html>
HTML5 Canvas • Canvas is a new element used for drawing graphics • Canvas has several straightforward methods for creating more interesting effects • Works well with Javascript for animations
HTML5 Canvas • Context • Uses an object that represents a 2d surface • Top left is 0,0 • Example: varcontext = example.getContext('2d'); • FillRect() • Fills a specified rectangle with a fill style context.fillStyle= "rgb(0,255,255)"; context.fillRect(horiz, vert, side1, side2 ); horiz and vert are the coordinates of the upper left corner side1 and side2 are the lengths of the sides • ClearRect() • SetInterval(function, interval) repeats a function at set time intervals. Interval is set in milliseconds
HTML5 Canvas <!DOCTYPE html> <head> <style type="text/css"> #example { border-style: solid; border-color: #000000; width: 900px; } </style> </head> <body> <canvas id="example" > This text is displayed if your browser does not support HTML5 Canvas. </canvas>
HTML5 Canvas <script type = "text/javascript"> var example = document.getElementById('example'); var context = example.getContext('2d'); varvmax = example.height; varhmax = example.width; varhoriz = 0; varvert = 0; var side = 30; var color = 0; context.fillStyle = "rgb(0,255,255)"; function draw() { context.clearRect(0,0, hmax, vmax); // color += 1; // context.fillStyle = "rgb("+color+","+color+","+ color+")"; if (horiz < (vmax - side)) vert+= 1; else if (vert > 0) vert -= 1; if (horiz < hmax) horiz += 1; context.fillRect(horiz, vert, side, side); } setInterval(draw, 20); </script> </body> </html>
Overview of Forms • Forms are used all over the Web to • Accept information • Provide interactivity • Types of forms: • Search form, Order form, Newsletter sign-up form, Survey form, Add to Cart form, and so on…
Overview of Forms (2) • Form • An HTML element that contains and organizes form controls such astext boxes, check boxes, and buttons that can accept information from Web site visitors.
Two Components of Using Forms 1. The HTML form -- the Web page user interface and 2. The server-side processing Server-side processing works with the form data and sends e-mail, writes to a text file, updates a database, or performs some other type of processing on the server.
HTML Using Forms • <form> tag • Contains the form elements on a web page • Container tag • The input controls for the form should all be inside the form element. • <input /> tag • Configures a variety of form elements including text boxes, radio buttons, check boxes, and buttons • Stand alone tag • <textarea> tag • Configures a text box • Container tag • <select> tag • Configures a select box (drop down list) • Container tag • <option> tag • Configures an option in the select box • Container tag
HTML <form> element • Example: <form name = "myForm" id = "myForm" method="post" action = "http://webdevfoundations.net/scripts/formdemo.asp"> • Form tag attributes: • action • Specifies the server-side program or script that will process your form data • method • get – default value, form data passed in URL • post – more secure, form data passed in HTTP Entity Body • name • Identifies the form • id • Identifies the form
HTML <form> element • Use one form element per form, with all the input controls inside it. Otherwise, the input will not all be submitted correctly.
HTML<input /> Text box • Accepts text information • Attributes: • type=“text” • name • id • size • maxlength • value
HTML<input /> Email box New in HTML5; best support in Chrome <form> Email: <input type="email" name="CustEmail" id="CustEmail" placeholder="me@example.com" /><br /> <input type="submit" /> </form>
HTML<input /> numeric range New in HTML5; best support in Chrome <form> Age: <input type="number" name="custAge" id="custAge" min = "0" max = "110" /> <br /> <input type="submit" /> </form>
HTML<input /> Password box • Accepts text information that needs to be hidden as it is entered • Attributes: • type=“password” • name • id • size • maxlength • value
HTML<input /> Check box • Allows the user to select one or more of a group of predetermined items • Attributes: • type=“checkbox” • name • id • checked • value
HTML<input /> Radio Button • Allows the user to select exactly one from a group of predetermined items • Each radio button in a group is given the same name and a unique value • Attributes: • type=“radio” • name • id • checked • value
HTML<input /> Radio Button <input type="radio" name="choice" id="yes" value = "yes" />Yes<br /> <input type="radio" name="choice" id="no" value = "no" />No<br />
HTML<select> Select List • Configures a select list (along with <option> tags) • Also known as: Select Box, Drop-Down List, Drop-Down Box, and Option Box. • Allows the user to select one or more items from a list of predetermined choices. • Attributes: • name • id • size • multiple
HTML<option> Options in a Select List • Configures the options in a Select List • Attributes: • value • selected
HTML<select> Select List Should beer be included in tuition? <select name ="freebeer"> <option value = "yes">Yes!</option> <option value = "no">No!</option> </select>
HTML<input> Submit Button • Submits the form information • When clicked: • Triggers the action method on the <form> tag • Sends the form data (the name=value pair for each form element) to the web server. • Attributes: • type=“submit” • name • id • value
HTML<input /> Reset Button • Resets the form fields to their initial values • Attributes: • type="reset" • name • id • value
HTML<input /> Button • Offers a flexible user interface • There is no default action when the button is clicked • Usually a JavaScript function is invoked when a button is clicked • Attributes: • type=“button” • name • id • value
HTML<input /> Hidden form data • This form control is not displayed on the Web page. • Hidden form fields • Can be accessed by both client-side and server-side scripting • Sometimes used to contain information needed as the visitor moves from page to page. • Attributes: • type=“hidden” • name • id • value
HTML<input /> Hidden form data <h3>Your Reply</h3> <form method = "post" action = "http://webdevfoundations.net/scripts/formdemo.asp"> <input type="hidden" name="postingid" value="98765"> name: <input name="realname" size=30> <br> email: <input name="email"> <br> subject: <input name="subject" value="re: hamlet and hesitation" size=30> <p> comments: <br> <textarea name="comments" cols="50" rows="10"> joe smiley wrote: I think hamlet doesn't act because if he does, the play's over. </textarea> <p> <input type="submit" value="send it!"> </form>
HTML Form Enhancements<label> Tag • Associates a text label with a form control • Two Different Formats:<label>Email: <input type="text" name="CustEmail" id ="CustEmail" /></label> Or<form> <label for="email">Email: </label> <input type="email" name="CustEmail" id="email" placeholder="me@example.com" /> </form>
HTML Form Enhancements<fieldset> & <legend> Tags • The Fieldset Element • Container tag • Creates a visual group of form elements on a web page • The Legend Element • Container tag • Creates a text label within the fieldset <fieldset><legend>Customer Information</legend> <label>Name: <input type="text" name="CustName" id="CustName" size="30" /></label><br /> <label>Email: <input type="text" name="CustEmail" id="CustEmail" /></label> </fieldset>
HTML Form Enhancementstabindex attribute • Attribute that can be used on form controls and anchor tags • Modifies the default tab order • Assign a numeric value <input type="text" name="CustEmail" id="CustEmail" tabindex="1" />
HTML Form Enhancementsaccesskey attribute • Attribute that can be used on form controls and anchor tags • Create a “hot-key” combination to place the focus on the component • Assign a value of a keyboard letter • On Windows use the ALT and the “hot-key” to move the cursor (worked for me in IE and Chrome, not in Firefox or Opera) <input type="text" name="CustEmail" id="CustEmail" accesskey=“c" />
HTML Form Enhancements<input /> Image Button • Submits the form • When clicked: • Triggers the action method on the <form> tag • Sends the form data (the name=value pair for each form element) to the Web server. • Attributes: • type=“image” • name • id • src
HTML Using a Table to Format a Form <form> <table border="0" width="75%"> <tr> <td align="right" width="10%">Name: </td> <td> <input type="text" name= "CustName" id= "CustName " size="30" /></td> </tr> <tr> <td align="right" width="10%">Email: </td> <td> <input type="text" name="CustEmail" id="CustEmail" /></td> </tr> <tr> <td align="right" width="10%"> <input type="submit" value="Submit" /></td> <td><input type="reset" /></td> </tr> </table> </form>
Using CSS to Style a Form • Moderate Approach • Use a table to format the form but configure styles instead of XHTML table attributes. table { border: 3px solid #000000; width: 100%;} td { padding: 5px; margin: 0px;} .mylabel { text-align: right;}
Using CSS to Style a Form “Pure" CSS Approach • Do not use a table to format the form. • Use CSS divs and spans with positioning properties to configure the page. #myForm {border:3px solid #000000; padding:10px;margin:10px;} .myRow {padding-bottom: 20px; .labelCol {float:left;width:100px; text-align:right;}
Form Example • formsample.html
Server-SideProcessing • Your web browser requests web pages and their related files from a web server. • The web server locates the files, may do other processing of data, and sends documents to your web browser. • The web browser then renders the returned files and displays the requested web pages for you to use.
Common Uses of Server-Side Scripting • Search a database • Place an order at an online store • Send a web page to a friend • Subscribe to a newsletter
CGICommon Gateway Interface • A protocol for a web server to pass a web page user's request to an application program and accept information to send to the user.
Server-Side Scripting • One of many technologies in which a server-side script is embedded within a Web page document saved with a file extension such as: • .php (PHP) • ..jsp (Sun JavaServer Pages) • .aspx (ASP.Net). • Uses direct execution — the script is run either by the Web server itself or by an extension module to the Web server.
Steps in Utilizing Server-Side Processing • Web page invokes server-side processing by a form or hyperlink. • Web server executes a server-side script. • Server-side script accesses requested database, file, or process. • Web server returns Web page with requested information or confirmation of action.
Server-Side Scripting Technologies • JavaServer Pages http://java.sun.com/products/jsp • ColdFusion http://www.adobe.com/products/coldfusion • PHPhttp://www.php.net • Ruby on Rails http://www.rubyonrails.org or http://tryruby.hobix.com • Microsoft’s ASP.NET Framework http://www.microsoft.com/net
ASP.NET Benefits • Good set of tools • Tools are well-integrated • Speedy performance (at least in development) • Easy dependency management • Easy design of basic GUIs and typical functionality • Easy (MS) Database Access