Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright Penny McIntire, 2007 Things to Do with JavaScript.

Similar presentations

Presentation on theme: "Copyright Penny McIntire, 2007 Things to Do with JavaScript."— Presentation transcript:

1 copyright Penny McIntire, 2007 Things to Do with JavaScript

2 2 copyright Penny McIntire, 2007 Things to Do with JavaScript Auto with reset confirm Auto with data validation and autofocusAuto with data validation and autofocus Rename a button Change background color View URL Set timer Open a window

3 3 copyright Penny McIntire, 2007 Things to Do with JavaScript Display date Print page Save and access a cookie Sniff the browser Data validation Preload images FlyoutDropdown menus

4 4 copyright Penny McIntire, 2007 Auto You can create an html form, then automatically the data captured from the form… JSAuto .html

5 Auto function allowReset() { return window.confirm(Clear the form?") }


Enter your first name: Enter your last name: Male Female I am retired action = mailto:… Executes the function allowReset to double check.

7 7 copyright Penny McIntire, 2007 Auto This confirms the reset before clearing the form. If the function returns true, it resets. If the function returns false, it does not. onSubmit works the same way, to interrupt the process before sending the form to the server.

8 8 copyright Penny McIntire, 2007 Auto Secret should never be used, and, in fact, modern versions of browsers dont allow secret ; a window is displayed asking user permission. There seems to be an upper limit of 2044 characters for the content of an auto .

9 9 copyright Penny McIntire, 2007 Auto with Editing and Autofocus Now lets simplify the part and check for empty fields as well as autofocus on appropriate fields… JSAuto 2.html

10 Auto with Editing and Autofocus function submitForm() { if (document.loginForm.firstName.value == "" && document.loginForm.lastName.value == "") { alert( "Please enter your first and last names"); document.loginForm.firstName.focus(); return false; } else if(document.loginForm.firstName.value == "") { alert( "Please enter your first name"); document.loginForm.firstName.focus(); return false; } else if(document.loginForm.lastName.value == "") { alert( "Please enter your last name "); document.loginForm.lastName.focus(); return false; } else { return true; } Return false; aborts the submit part of autosubmit. Return true; submits the . Sets focus on empty field.

11 Auto with Editing and Autofocus Enter your first name: Enter your last name: onSubmit captures the submit and sends it to the function. A false return aborts the auto . Sets focus on first field, when page opens.

12 12 copyright Penny McIntire, 2007 Rename a Button JSRenameButton.html

13 function getButtonName() { var newName = prompt("Rename the button, please") document.myForm.myButton.value=newName } Tracing through the DOM Replaces the old value with the new one. Sets up the button.

14 14 copyright Penny McIntire, 2007 Change Background Color JSChangeBackground.html

15 function newBackColor(color) { if (color == 1) {document.bgColor="red"} if (color == 2) {document.bgColor="green"} if (color == 3) {document.bgColor="blue"} } We love color! To change the background color, click on a button below: Changes the document property bgColor

16 16 copyright Penny McIntire, 2007 View URL JSViewURL.html

17 function getURL () { alert(You are in document: " + document.location) } We love URLs! To find out your current URL:

18 18 copyright Penny McIntire, 2007 Timer JSTimer.html

19 var myTimer function launchTimer() { myTimer = setTimeout("alert('Sorry, your time is up!')",5000) } You must do something on this page within 5 seconds! launchTimer()

20 20 copyright Penny McIntire, 2007 Open a Window JSOpenNewWindow.html

21 function myNewWindow() {"", "newWindow", "height=100,width=100,status=yes"); newWindow.focus(); } Open New Window (URL, window name, parameters) See page 885 of DHTML.

22 22 copyright Penny McIntire, 2007 Display Date JSDisplayDate.html

23 var myDate = new Date(); var myDay=myDate.getDate(), //day number in month myDayOfWeek=myDate.getDay(), //day number in week myMonth=myDate.getMonth(), //month number myYear=myDate.getFullYear(); //year number switch (myDayOfWeek) { case 0: dayName = "Sunday"; break; case 1: dayName = "Monday"; break; case 2: dayName = "Tuesday"; break; case 3: dayName = "Wednesday"; break; case 4: dayName = "Thursday"; break; case 5: dayName = "Friday"; break; default: dayName = "Saturday"; break; } If new Date with no constructors, it gets todays date. getYear method is unpredictable in earlier browsers because of Y2K

24 switch (myMonth) { case 0: monthName = "January"; break; case 1: monthName = "Februrary"; break; case 2: monthName = "March"; break; case 3: monthName = "April"; break; case 4: monthName = "May"; break; case 5: monthName = "June"; break; case 6: monthName = "July"; break; case 7: monthName = "August"; break; case 8: monthName = "September"; break; case 9: monthName = "October"; break; case 10: monthName = "November"; break; default: monthName = "December"; }

25 document.writeln("The system date looks like this..." + myDate +" ") document.writeln("Today's date is: " + dayName + ", " + monthName + " " + myDay + ", " + myYear + " ")

26 26 copyright Penny McIntire, 2007 Printing a Page To print a page… Print this page

27 27 copyright Penny McIntire, 2007 Cookies As your web application executes, and perhaps links from page to page, it can be important to keep track of various pieces of information. For instance, a user might log in on the first page, and you might want to display that users name on a page he or she links to later.

28 28 copyright Penny McIntire, 2007 Cookies Traditional (non-web) applications have no trouble with this keeping state or handling persistence – they arent hopping from page to independent page, and even if they were, they would simply write important data out to disk.

29 29 copyright Penny McIntire, 2007 Cookies Its not so easy with web applications. –Each document is independent and cant easily communicate with another document. –Writing out to disk means hitting the server, which is time consuming, and it might mean storing thousands of entries on the server per day, which eats up disk space rapidly.

30 30 copyright Penny McIntire, 2007 Cookies So, we have cookies. Think of them as special, hidden cookie jars, kept on the client, in which the client or the server can drop tidbits of information for later retrieval. Examples of cookie use: –When a web site remembers your name or password. –When a shopping cart remembers what you have already chosen as you browse from page to page.

31 31 copyright Penny McIntire, 2007 Cookies There are some limitations to cookies: –A web browser can retain a maximum of only 300 cookies. –Only 20 may be owned by a given domain (i.e., could store only 20 cookies on your machine). –Each cookie must be under 4K in length, 2K for older browsers.

32 32 copyright Penny McIntire, 2007 Cookies You can open your cookies file… –In IE, under Temporary Internet Files

33 33 copyright Penny McIntire, 2007 Cookies Cookies store: –The domain name that created the cookie. –Whether the server or JavaScript within the browser stored the cookie. –Whether or not the cookie should be transported using SSL. –The expiration date. –The name and value pair that you want to store in the cookie…

34 34 copyright Penny McIntire, 2007 Cookies –For instance, if you want to save the users first name, you might have a name/value pair like firstName= Penny.

35 35 copyright Penny McIntire, 2007 Cookies The format of the string used to set a cookie is as follows: document.cookie = myNewCookie=value you have assigned + expires=Friday, 30-Mar :59:59 GMT; –This leaves a cookie with the name myNewCookie, with the value value you have assigned, which expires on March 30 at 1 second before midnight. –Lets look at the individual parts here…

36 36 copyright Penny McIntire, 2007 Cookies myNewCookie=value you have assigned name=value you have assigned –The value part is required. –No semicolons, commas, or blanks in the whole name part, OR Encode the value part of the pair with the JavaScript escape() function to take care of any special characters. Use the unescape() function later, when retrieving the cookie, to get back to the original characters. –If two cookies have the same name, the new one overwrites the old one.

37 37 copyright Penny McIntire, 2007 Cookies expires=Friday, 30-Mar :59:59 GMT; –Can use the JavaScript Date object format shown here, or use a calculation for the date and time in milliseconds since January 1, 1970, GMT. –If this is omitted, the cookie expires automatically at the end of the session.

38 38 copyright Penny McIntire, 2007 Saving a Cookie Example and code from W3C: asp

39 39 copyright Penny McIntire, 2007 Sniffing the Browser and Version There are lots of scripts floating around to check for the browser type and version. If you check this, you can write completely different versions of the page for the different browsers. But, is this a good thing??? –A maintenance nightmare to avoid whenever possible.

40 40 copyright Penny McIntire, 2007 Sniffing the Browser and Version If you really must include something for a particular browser, if might be better to have just a single document, with small areas that check for the browser, rather than having two completely different pages.

41 41 copyright Penny McIntire, 2007 Sniffing the Browser and Version Can instead check to see if a particular feature that youre using is supported – less of a maintenance problem. Example: if (document.layers) //Navigator <6.0 In any case, there is code out there that you can download to do the sniffing. –Check

42 42 copyright Penny McIntire, 2007 Data Validation Your task is to anticipate every possible entry that users could make and let through only those your scripts can use. Danny Goodman Also, use checkboxes and radio buttons whenever possible, to ensure that invalid data entry doesnt occur.

43 43 copyright Penny McIntire, 2007 Data Validation Verifying just before the form is submitted: –Use the onSubmit = event on the form to execute a function that does final validation. Example: checking if a field is all digits…

44 function isDigits(inString) { if(inString.value.length == 0) return false; for(i=0; i < inString.length ; i++) { char = inString.substring(i, i+1); if (char > "9" || char < "0") return false; } return true; }

45 45 copyright Penny McIntire, 2007 Preloading Images Preload images –When a user mousesover a button for the first time, the mouseover effect could take a while to show up if it has to wait for the image to download. –As an alternative, download all of the images for the page when the page itself downloads, so they are all ready and waiting. –Code for this is automatically done by Dreamweaver unless you deliberately tell it not to add the code…

46 46 copyright Penny McIntire, 2007 Preloading Images function MM_preloadImages() Good luck understanding Dreamweavers code. Nonetheless, it works. Essentially, onLoad in the calls the function to reference each image that doesnt appear on the original page (like rollover images), which forces a download of the image right then, instead of waiting until the user mouseovers.

47 47 copyright Penny McIntire, 2007 Flyout/Drop Down Menus Two easy (?) ways to do this: –Go to The JavaScript Source and copy in one of their cross-browser scripts. –Use Dreamweavers Show/Hide Layers; i.e., create semi-manually. –Use Dreamweaver Window > Behaviors to create automatically. Fairly reliable in last couple of versions? Still need to tweak the CSS so that the dropdowns fit with the look of your page.

48 48 copyright Penny McIntire, 2007 Resources bfuscator.htm address obfuscator, so that spammers cant harvest addresses from mailto: links. bfuscator.htm Example of how RSS can be used.

Download ppt "Copyright Penny McIntire, 2007 Things to Do with JavaScript."

Similar presentations

Ads by Google