javascript events with xhtml please use speaker notes for additional information!

24
JavaScript Events with XHTML Please use speaker notes for additional information!

Upload: mervin-morrison

Post on 14-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JavaScript Events with XHTML Please use speaker notes for additional information!

JavaScript Events with XHTML

Please use speaker notes for additional information!

Page 2: JavaScript Events with XHTML Please use speaker notes for additional information!
Page 3: JavaScript Events with XHTML Please use speaker notes for additional information!
Page 4: JavaScript Events with XHTML Please use speaker notes for additional information!

<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>JavaScript Rollover</title><style type="text/css">.alignright { text-align: right; }.fontverylarge { font-size: 36pt; }</style></head><body><p class="alignright"><a href="#" class="fontverylarge" onmouseover = "alert('Testing mouse over!');">Mouse over here!</a></p>

The first thing to note is the use of a href=“#”.This means that the same page should be loadedif you click on the words Mouse over here!

The class establishes the size of thewords Mouse over here! as 36 pt.

The event onmouseover means that if a mouse is rolled over the words Mouse over here! The alert box will be displayed with the message Testing mouse over!

This code means that I do not have to tell the encoding when I validate. You should start to use these 4 lines in your code.

Page 5: JavaScript Events with XHTML Please use speaker notes for additional information!

<p>Some of the first interesting things you can do with JavaScript are event-driven. Eventdriven means that a particular action causes something to happen. JavaScript has clicks that can detect events such as onclick, onmouseover, onmouseout. This page illustrates the mouse over and mouse out events.<br /><br /><br /></p>

<p><a href="#" onmouseover="document.CISimage.src='CISa.gif';" onmouseout="document.CISimage.src='CISb.gif';"><img src="CISa.gif" alt="CIS" name="CISimage" /></a><br /><br /></p><p>Notice that the # means this page to HTML and so setting the HREF to the # means stay onthe same page. You can also use the name of the current page.<br />

In this example, there is a onmouseover and a onmouseout event. If the onmouseover event happens then the CISa.gif image is loaded. If the onmouseout event happens then the image CISb.gif is loaded. Note that img src established what image should be there when the page is loaded. Name names the area, it does not validate strict which is why I validated transitional. When I changed to id, which is more acceptable, this did not run on some reasonably recent browsers. So instead of making the change, I chose to validate transitional.Now let’s look at the command: document.CISimage.src=‘CISa.gif’;Read this as CISa.gif is the source of the area named CISimage on the document. The document is the part of the window that holds your page.

Page 6: JavaScript Events with XHTML Please use speaker notes for additional information!

Now looking at the construction of the command. Note that onmouseover should be writtenexactly that way because of case sensitivity issues related to XHTML. In regular HTML, it was written as onMouseOver. In the first example, I am sayingthe mouse rolls over the words Mouse over here!, then the alert box will come up with themessage that says Testing mouse over. Notice the use of the double quote, single quotes,parenthesis and semi-colon. Inside the double quotes is the JavaScript command whichshould end with a semi-colon. What we are doing is putting the JavaScript inside thequotes of an event. The event is onmouseover. The alert needs a message and the messageinside parenthesis and enclosed in single quotes. If you have quotes within quotes the inner quotes must be single to differentiate. The alert() function puts the message inthe () inside the message box. Note that all functions in JavaScript have the form functionName() where sometimes the parenthesis contain data and sometimes they are empty.We will see more of this in later examples. Note one last thing that has nothing to dowith JavaScript, because I wanted to Mouse over here! to be large and aligned to the right,I used the paragraph to align to the right and the font to change the font. Theseare not needed for the JavaScript.</p><p>On the next example, I am using both onmouseover and onmouseout. I went into PhotoEditorand did a invert with the CISa.gif image and saved it as CISb.gif. Then I can have oneimage show when the mouse rolls over the image and a second image show when the mouse rollsout.</p><p>When I work with an image, I need to be aware of the fact that frequently browers will notlet the developer put events inside image tags. So what you have to do is set up the HREFequal to # and put the events inside that and then set up the image source with name asshown above. When the image is firstloaded it will show the image in IMG SRC which in thiscase is CISa.gif. When the mouse goes over that same image is shown. When the mouse goes outa different image is shown (in this case the inverted image).</p> <p>The dots in document.CISimage.src have special meaning to JavaScript. It will goto the last dot and work backward in the interpretation. So this essentially reads as change the src of CISimage which is in the document. This follows a document object model hierarchy (DOM) which can be referenced in many tutorials on JavaScript.<br /> To make things more efficient you can preload the images. I will illustrate a preload inanother example.</p><p>One other thing to note - because single and double quotes have special meanings, you cannotuse them in the ordinary way in your text. If you want to use the quotes in your text youdo it with Jane\'s where the \ tells JavaScript to use the single quote instead of interpreting it.</p></body></html>

Page 7: JavaScript Events with XHTML Please use speaker notes for additional information!
Page 8: JavaScript Events with XHTML Please use speaker notes for additional information!

<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>JavaScript Rollover - another example</title><style type="text/css">.borderno { border: none; }</style><script type="text/javascript"> var CISa_image = new image();CISa_image.src = "CISa.gif";var CISb_image = new image();CISb_image.src = "CISb.gif";</script></head><body><p>The code above preloads two images.<br /><a href="#" onmouseover="CISimage.src='CISa.gif';" onmouseout="CISimage.src='CISb.gif';"><img src="CISb.gif" class="borderno" name="CISimage" alt="CIS" /></a></p></body></html>

Here I am using JavaScript in the head to preload the images. This means that the images would load faster if this was an extensive page.

The borderno class is set up to have no borders, so when I use it, I do not see the blue lines around the image.

I did not use document here, document is assumed.

Page 9: JavaScript Events with XHTML Please use speaker notes for additional information!
Page 10: JavaScript Events with XHTML Please use speaker notes for additional information!

<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>Java rotate</title><style type="text/css">body { background: beige; }div { text-align: center; }</style><script type="text/javascript"> var originalz = new image(); originalz.src = "house.jpg"; var flipz = new image(); flipz.src = "houseusd.jpg";</script></head><body><div><h1>Here is an image to roll the mouse over</h1><h3>Rolling the mouse over and out are called events.</h3><a href="javamousex.html" onmouseover="document.snowhouse.src='house.jpg';" onmouseout="document.snowhouse.src='houseusd.jpg';"> <img src="houseusd.jpg" name="snowhouse" width="577" height="384" alt="house" /></a></div></body></html>

Page 11: JavaScript Events with XHTML Please use speaker notes for additional information!

<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>Button Click</title></head><body><p>This uses a button that the user can click on to raise an alert.</p><form action="#"><input type="button" value="Click" onclick="alert('Hello World');" /></form></body></html>

This shows the onclick event associated with a form button.

Page 12: JavaScript Events with XHTML Please use speaker notes for additional information!
Page 13: JavaScript Events with XHTML Please use speaker notes for additional information!

<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>Events in Java</title><style type="text/css">p { text-align: center; }</style></head><body><h3>Events are a different type of code because they are built directly into the HTML code. They aren't scripts because their design was to be embedded in the HTML. First I am going to illustrate mouse over/out events.</h3><p> <a href="tryevent1x.html" onmouseover="alert('Testing events...')">ALERT MOUSE TEST!</a>

Page 14: JavaScript Events with XHTML Please use speaker notes for additional information!

<a href="tryevent1x.html" onmouseover="document.bgColor='beige'">CHANGE BACKGROUND TO BEIGE!</a><br /><br /><a href="tryevent1x.html" onmouseover="document.bgColor='aqua'">CHANGE BACKGROUND TO AQUA!</a>

Page 15: JavaScript Events with XHTML Please use speaker notes for additional information!

<a href="tryevent1x.html" onmouseover="document.bgColor='pink'" onmouseout="document.bgColor='green'">CHANGE BACKGROUND!</a>

Page 16: JavaScript Events with XHTML Please use speaker notes for additional information!

<a href="tryevent1x.html" onmouseover="window.status='Testing roll over'; return true" onmouseout="window.status='Testing roll out'; return true">WINDOW STATUS TRUE TEST!</a>

Page 17: JavaScript Events with XHTML Please use speaker notes for additional information!

<a href="tryevent1x.html" onmouseover="window.status='Testing roll over'" onmouseout="window.status='Testing roll out'">WINDOW STATUS TEST!</a>

Page 18: JavaScript Events with XHTML Please use speaker notes for additional information!

<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>Events in Java</title><style type="text/css">p { text-align: center; }</style></head><body><h3>Events are a different type of code because they are built directly into the HTML code. They aren't scripts because their design was to be embedded in the HTML. First I amgoing to illustrate mouse over/out events.</h3><p><a href="tryevent1x.html" onmouseover="alert('Testing events...')">ALERT MOUSE TEST!</a><br /><br /><a href="tryevent1x.html" onmouseover="document.bgColor='beige'">CHANGE BACKGROUND TO BEIGE!</a><br /><br /><a href="tryevent1x.html" onmouseover="document.bgColor='aqua'">CHANGE BACKGROUND TO AQUA!</a><br /><br /><a href="tryevent1x.html" onmouseover="document.bgColor='pink'" onmouseout="document.bgColor='green'">CHANGE BACKGROUND!</a><br /><br /><a href="tryevent1x.html" onmouseover="window.status='Testing roll over'; return true" onmouseout="window.status='Testing roll out'; return true">WINDOW STATUS TRUE TEST!</a><br /><br /><a href="tryevent1x.html" onmouseover="window.status='Testing roll over'" onmouseout="window.status='Testing roll out'">WINDOW STATUS TEST!</a><br /><br />Note: The return true clause allows the default in the window status to be over ridden.</p></body></html>

Page 19: JavaScript Events with XHTML Please use speaker notes for additional information!
Page 20: JavaScript Events with XHTML Please use speaker notes for additional information!

<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>Events in Java</title><style type="text/css">body { background: pink; }p { text-align: center; }</style></head><body onload="alert('WELCOME!')"><h3>This example illustrates the on click event.</h3><p> <a href="tryevent2.html" onclick="alert('Testing events...')">ALERT ON CLICK TEST!</a><br /><br /></p><h3>Using onFocus, when you click on the box in the form, the window will display the activity to be done.</h3><h3>Using onchange tells when the data has been changed.</h3><form id="crsform" action="#">Course information:<input type="text" size="5" id="crscode" onfocus="window.status='Enter course code'" /><input type="text" size="25" name="crsname" onfocus="window.status='Enter course name'" onchange="alert('The data has been changed')" /><br /><br />MAJOR:<br />

Page 21: JavaScript Events with XHTML Please use speaker notes for additional information!

<input type="radio" name="major" value="CI" />Computer Information Systems<br /><input type="radio" name="major" value="BU" />Business Administration<br /><input type="radio" name="major" value="other" />Other major <br /><h3>The onBlur means that you have lost focus on an answer.</h3><input type="text" size="25" name="crsinst" onfocus="window.status='Enter instructor name'" onblur="alert('Did you enter instructor name?')" /><br /><br />Comments:<br /><textarea rows="6" cols="40" name="Comments"></textarea><input type="reset" value="Clear" /> <input type="button" value="Back" onclick="history.go(-1)" /><input type="button" value="Forward" onclick="history.go(1)" /></form></body></html>

Page 22: JavaScript Events with XHTML Please use speaker notes for additional information!
Page 23: JavaScript Events with XHTML Please use speaker notes for additional information!

<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>More events</title><script type="text/css">var mycolor;var myfont;</script></head><body><h2>This shows some uses of the onclick event</h2><div><img src="CISa.gif" name="originalz" alt="CIS" /><br /><a href = "#" onclick="window.document.bgColor='beige'">Make background beige</a><br /><a href = "#" onclick="window.document.fgColor='blue'">Make font blue</a><br /><a href="#" onclick="mycolor=prompt('Enter your choice of background color',''); window.document.bgColor=mycolor;">Change the background color</a><br /><a href="#" onclick="myfont=prompt('Enter your choice of font color',''); window.document.fgColor=myfont;">Change the font color</a><br /><a href="#" onclick="document.originalz.src='CISb.gif';">Click here to change the top image!</a><br /><a href="#" onclick="document.originalz.src='CISa.gif'; return false;">Click here to change the top image back!</a><br />

Page 24: JavaScript Events with XHTML Please use speaker notes for additional information!

Note: Return false is another way to tell the browser not to follow the link given.<br />The Prompt puts up a box where the user can enter input. Note that in the prompt I have some words that will become the title, then I entered a comma and two quotes together. Thetwo quotes together make the box where I want the user to enter information blank. IfI wanted to put a default in the user entry line I could put in between the quotes.<br /><a href="#" onclick="mycolor=prompt('Enter your choice of background color','pink'); window.document.bgColor=mycolor;">Change the background color</a><br />Note also that there are two things that I want to do on the click event, one is to getthe user to pick a color or in this case, go with the default. The other is to use thatcolor to change the background. The double quotes are around both of these.</div></body></html>