Download JavaScript and Advanced HTML: A Comprehensive Guide by Ye Wu and more Study notes Software Engineering in PDF only on Docsity!
JavaScripts and Advanced HTML
Ye Wu http://www.ise.gmu.edu/~wuye/ SWE 642 Software Engineering for the World Wide Web
sources: Java for the Web with Servlets, JSP and EJB, Kurniawan, New Riders HTML 4 for the World Wide Web, Castro, Peachtree Press Core Web Programming, Hall, PTR
Java Scripts
• JavaScript is not Java
• JavaScript is a scripting language:
- Embedded in HTML
- Interpreted as the page is loaded
- Can manipulate the HTML page
• Is executed client site
- Serverside Javascript: Netscape’s Livewire allows JavaScript to be used for CGI purposes. We will not discuss this.
• No type checking, but is case sensitive.
9/9/2003 © Dr. Jeff Offutt and Dr. Ye Wu 3
Why and why not JavaScript?
- What can be done with JavaScript at client side and can not be done with other techniques at server side? - Monitor user events and take action - Some dynamic effects
- What can be done at both client and server side, but better to be used with JavaScript? - Build HTML dynamically when page is loaded - Interactive web pages - Data verification
- What are the drawbacks of JavaScript?
- Platform dependent
- Can be turned off
- Performance
Where does JavaScript Go and How?
• Between and or and
• Some “script” calls may be embedded in the HTML
tags.
• Comments: // -- single-line and /* */ Multi-line
9/9/2003 © Dr. Jeff Offutt and Dr. Ye Wu 7
First Example
• How do we deploy JavaScript components?
• How do we obtain data and how do we export data?
• How do we monitoring user events, and how do we
take actions?
• Pay attention to those statements
- document.myForm.sum.value
- onclick=add()
- parseInt()
http://www.ise.gmu.edu/~wuye/classes/642/Examples/javascript/unix/cal.html
Browser Object Model (BOM)
window Object
location Object history Object navigator Object screen Object
BOM – collection of objects that the browser makes available to us for use with JavaScript.
document Object
form Object link Object … … image Object
document Object
form Object link Object … … image Object
DOM
9/9/2003 © Dr. Jeff Offutt and Dr. Ye Wu 9
Use BOM objects
• window object is a global object.
• Addressing is done by separating object, methods, and
properties with dots.
- window.alert(“Hello”);
- alert(“Hello”);
• There are also alternative way to accessing the objects.
- document.forms[0].elements[0].value
- document.myform.nameinput.value
- document.myform[“nameinput”].value
More examples on BOM/DOM
• Window.open, window.close, confirm, alert
• History.go(-1) history.go(0)
• Window.location.replace(“”), location.href=“”;
• Window.Navigator.appName navigator.appVersion
navigator.cokieEnabled, navigtor.userAgent
• Window.screen.height, screen.width
• Window.document.
• this.value
http://www.ise.gmu.edu/~wuye/classes/642/Examples/javascript/unix/BOMDOM_Example.html
9/9/2003 © Dr. Jeff Offutt and Dr. Ye Wu 13
More Events
• Clicking on a button
• Clicking on a link
• Exiting a page
• Moving onto or off of a link
• Input focus on a FORM element
• Submitting CGI
• Image loading error
Validating Forms
• Valid each input fields (text, radio, and etc)
• Submitting CGI Event
9/9/2003 © Dr. Jeff Offutt and Dr. Ye Wu 15
Cascading Style Sheets (CSS)
Cascading Style Sheet (CSS) is a simple
mechanism for adding style (e.g. fonts,
colors, spacing) to Web documents.
Cascading Style Sheets (CSS)
• Separate the content of the HTML page from its
presentation.
Ex. 1. We might want all paragraph tags,
, to have the color dark blue. What if we want to change it to pink? Ex. 2. What if we want all the links change the text color while the mouse move over them.
• Appearance control
- Control the text in ways that are out of reach of HTML tags
- Special tags, such as allow more flexible appearance control.
9/9/2003 © Dr. Jeff Offutt and Dr. Ye Wu 19
Contextual Selector
Context selector is a selector that addresses specific
occurrence of an element. It is a string of individual
selectors separated by white space, a search pattern,
where only the last element in the pattern is addressed
providing it matches the specific context.
P.intro B {color:red}
The is the most important part.
Contextual Selector
• Other Selector
- Ancestor-child Selector P B {color : red}
- Parent-child Selector P > B {color : red}
- first-child Selector P:first-child {color : red}
- Sibling Selector P + B {color : red} - Link element a:link B {color : red} a:visited B {color : red} a:focus B {color : red} a:hover B {color : red} a:active B {color : red} - first-line Selector P:first-line {color : red} - first-letter Selector P:first-letter {color : red}
9/9/2003 © Dr. Jeff Offutt and Dr. Ye Wu 21
Attribute Selector
- A [title] {text-decoration:underline}
- A [title =“attribute value”] {text-decoration:underline}
- A [title ~= “attribute”] {text-decoration:underline}
- A [title |= “attribute”] {text-decoration:underline}
How to apply CCS (2)
• External style sheet
• Inline style – through style attribute
CSS
9/9/2003 © Dr. Jeff Offutt and Dr. Ye Wu 25
DIV/SPAN Tags
• User-defined tag
• Appearance management
• Block-level tags – begin on a new line and can contain
other block level elements.
• Inline tags – usually do not begin a new line, and can
only contain other inline elements or text.
DIV/SPAN Tags
- DIV – block-level tag – allows you to create areas on the page. (DIV tags are commonly used to create elements on the page that will be manipulated independently of the rest of the page.
It’s a DIV
- SPAN – inline-tag – used to apply a certain type of style sheet to an area of text or graphics without any line breaks.
9/9/2003 © Dr. Jeff Offutt and Dr. Ye Wu 27
DIV and JavaScript
- Transition
Between pages
- In page (filters) function show() { text.filters(0).apply(); text.style.visibility = "visible"; text.filters(0).play(); } ……..
Show how this text slowly reveals!!
http://www.ise.gmu.edu/~wuye/classes/642/Examples/javascript/unix/PageTransition.html http://www.ise.gmu.edu/~wuye/classes/642/Examples/javascript/unix/InPageTransition.html http://www.ise.gmu.edu/~wuye/classes/642/Examples/javascript/unix/OtherFilters.html
Miscellaneous
• Timer in a web page
ID=setTimeout(“your JS code”,millisenconds_delay);
• Using ActiveX and Plug-ins with JavaScript
- Plug-ins are objects that encapsulate all the functionality they need to perform their tasks.
- ActiveX controls cannot run on their own, the need to be sited in a container program.
- Checking for plug-ins. Netscape 3 and above has a navigator.plugins array (IE does not).
http://www.ise.gmu.edu/~wuye/classes/642/Examples/javascript/unix/plugin.html http://www.ise.gmu.edu/~wuye/classes/642/Examples/javascript/unix/timer.html
JavaScript Tips
- Build your script a piece at a time
- Store to files and load with HTML files
- Do not change variable types, do not use same name for global and local variables
- Remember, JavaScript is case sensitive
- Debugger
- Microsoft IE
- http://msdn.microsoft.com/scripting
- Netscape Navigator
- JavaScript console
- JavaScript debugger: http://developer.netscape.com/software/jsdebug.html (out-of-date)