Agenda: Fundamentals of Web-Centric Development
Agenda: Fundamentals of Web-Centric Development
What is DHTML and what is it used for? DOM and the Cross Browser Issues Properties og Collections Dynamic style, content and position Events Examples AJAX
NOTICE:
It is not possible to cover all possibilities of modern DHTML
Slide 2
Ingenirhjskolen i rhus
DHTML
DHTML = Dynamic HTML Client Side NOT a W3C Recommendation DHTML = (X)HTML + CSS + JavaScript + DOM
HTML (4.0+), CSS (1.0+), DOM (1+)
XHTML, CSS, JavaScript and DOM has already been introduced Unifying these leads to dynamic content
Slide 3 Ingenirhjskolen i rhus
Validation of user input (already displayed during JS) Minimize Server Load Dynamic menu structures More stable GUI (AJAX) Dynamic Help Rollover graphics Event controlling = Windows-like user interface -> More possibilities e.g. browser based HTML editor Alternatives: Flash/Shockwave, Java Applets, ActiveX and other plug-in technologies
Slide 4 Ingenirhjskolen i rhus
DOM
DOM: Document Object Model
http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML20030109/
W3C:
Standard for accessing structured doucments Core DOM used for XML HTML DOM used for HTML Representation of a document as a tree structure APIs for e.g. JavaScript, Java, C++, C#, CLS
Ingenirhjskolen i rhus
Slide 5
Ingenirhjskolen i rhus
Slide 6
Slide 7
Ingenirhjskolen i rhus
Slide 8
Ingenirhjskolen i rhus
DOM level 1:
Recommandation in October 1998 All HTML & XML elementer are objects in a trestructure May be manipulated with and added to
DOM level 2:
Recommandation Novemeber 2000 Event handling included. (eg OnClick), XML Namespaces
DOM level 3
Extensions and added standardization of more of the existing functionalities
Slide 9
Ingenirhjskolen i rhus
Slide 10
Ingenirhjskolen i rhus
IE4:
<script language="JavaScript" type="text/JavaScript"> function aFunction(){ document.all.ElmRef.left=300; document.all.ElmRef.top=300; } </script> <div id=ElmRef>A XHTML DIV Container ELement</layer>
Warning:
- This is data from Wikipedia.com -http://en.wikipedia.org/wiki/Comparison_of_layout_engines -Please check references! -Check vendors
Slide 11
Ingenirhjskolen i rhus
Slide 12
Ingenirhjskolen i rhus
Browser Detect:
If (IE) {} else if (NetScape) {}
Object Detect:
if (document.images)
{ do something with the images array }
if (window.focus)
window.focus()
Slide 13 Ingenirhjskolen i rhus Slide 14 Ingenirhjskolen i rhus
children collection
All HTML elements nested in a given element
forms collection
All Form elements in a given element
frames collection
Ussed for frame access
Slide 17
Ingenirhjskolen i rhus
Slide 18
Ingenirhjskolen i rhus
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!-- Fig 13.2: all.html --> <!-- Using the all collection --> <html xmlns = "http://www.w3.org/1999/xhtml"> all <head> <title>Object Model</title> <script type = "text/javascript"> <!-var elements = "";
All.html
The for loop loops through the elements of the collection and display each elements name.
Program Output
The length property of the all collection specifies the number of elements in the collection.
function start() { for ( var loop = 0; loop < document.all.length; ++loop ) elements += "<br />" + document.all[ loop ].tagName; pText.innerHTML += elements; alert( elements ); } // --> </script> </head> The all collection
The name of each XHTML element (given in the tagName property) in the collection is appended to elements.
<body onload = "start()"> <p id = "pText">Elements on this Web page:</p> </body> </html>
is a collection of all the XHTML elements in the page in the order they appear.
The XHTML elements are rendered on the page. Note comments are represented as !.
An alert dialog is displayed with all the names of the XHTML elements in the all collection.
The innerHTML property is similar to the innerText property but can also include XHTML formatting.
Slide 19 af 51 Ingenirhjskolen i rhus Slide 20 af 51 Ingenirhjskolen i rhus
Dynamic Style
Changing an elements style dynamic
<script > document.all.para1.style.color=red; document.all.para1.style.backgroundColor=white; document.all.para1.style.className=smallFonts; </script> <p id=para1>This is a text</p>
Slide 21
Ingenirhjskolen i rhus
Slide 22
Ingenirhjskolen i rhus
Dynamic Content
Content may be changed dynamic
<script > document.all.para1.innerText=a new text; document.all.para1.innerHTML=<i>a new text</i>; </script> <p id=para1>This is content which will be changed</p>
Dynamic Positioning
We may change the x,y position dynamically
<script > document.all.afs1.style.left=200px; </script> <div id=afs1 style=position:absolut;left:20;top> This section may be moved dynamically</div>
Frames
Usefull Frames collection Parent for moving up and Frames for down
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> <!-- Fig. 13.7: index.html --> <!-- Using the frames collection --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Frames collection</title> </head> <frameset rows = "100, *"> <frame src = "top.html" name = "upper" /> <frame src = "" name = "lower" /> </frameset> </html>
Index.html
Slide 25
Ingenirhjskolen i rhus
Slide 26 af 51
Ingenirhjskolen i rhus
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!-- Fig. 13.8: top.html --> <!-- Cross-frame scripting --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>The frames collection</title>
<script type = "text/javascript"> <!-function start() { var text = prompt( "What is your name?", "" ); parent.frames( "lower" ).document.write( "<h1>Hello, " + text + "</h1>" ); } // --> </script> The write function </head> <body onload = "start()"> <h1>Cross-frame scripting!</h1> </body> </html>
is used to write text to the frame in the browser. Browser updated with user name and Hello displayed in bottom frame.
The parent frame of the current frame is first referenced following that the lower frame is referenced.
DHTML Events
Similar to Windows Event Model Events can capture user / browser interaction
Mouse movements / clicks, onclick, onmouseover, onfocus and more
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!-- Fig. 14.2: onload.html --> <!-- Demonstrating the onload event --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>DHTML Event Model - onload</title> <script type = "text/javascript"> <!-var seconds = 0;
Onload.html
Function startTimer will call function updateTime every 1000 milliseconds.
function startTimer() { invoke // 1000 milliseconds = 1 second window.setInterval( "updateTime()", 1000 ); } function updateTime() { seconds++; soFar.innerText = seconds; } // --> </script> </head> <body onload = "startTimer()">
Function updateTime sets the innerText property of the element with soFar as an id to the number of seconds that have elapsed since loading.
<p>Seconds you have spent viewing this page so far: <a id = "soFar"><strong>0</strong></a></p> </body> </html>
Slide 30 af 51
Slide 29
Ingenirhjskolen i rhus
Ingenirhjskolen i rhus
Event Bubbling
Program Output
All events bubbles from a child element up to its parent element and continues to root element This behavior may be changed:
event.cancelBubble=true
The page will dynamically update the number of seconds that have elapsed since the page has loaded every second.
Slide 31 af 51
Ingenirhjskolen i rhus
Slide 32 af 51
Ingenirhjskolen i rhus
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!-- Fig 14.9: bubbling.html --> <!-- Disabling event bubbling --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>DHTML Event Model - Event Bubbling</title> <script type = "text/javascript"> <!-function documentClick() { alert( "You clicked in the document" ); } function paragraphClick( value ) { alert( "You clicked the text" ); if ( value ) event.cancelBubble = true; } document.onclick = documentClick; // --> </script> </head>
Bubbling.html
32 33 34 35 36 37
<body> <p onclick = "paragraphClick( false )">Click here!</p> <p onclick = "paragraphClick( true )">Click here, too!</p> </body> </html>
Bubbling.html
Program Output
Clicking on the first p element triggers line 27 because the onclick event has bubbled up to the document level.
Clicking on the second p element passes a value of true to function paragraphClick, which will disable the event bubbling for this event.
T he ev ent ha s be en c anc e led
Slide 33 af 51
Ingenirhjskolen i rhus
Slide 34 af 51
Ingenirhjskolen i rhus
DHTML Events I
Ev e n t D e sc rip t io n
DHTML Events II
Ev e n t D e sc rip t io n
C lipb o a rd even ts onbeforecut onbeforecopy onbeforepaste oncopy oncut onabort onpaste D a ta b ind in g events onafterupdate onbeforeupdate oncellchange ondataavailable ondatasetchanged ondatasetcomplete onerrorupdate onrowenter onrowexit onrowsdelete onrowsinserted Fig . 1 4 .1 0 D y n a m ic
Fires b efo re a selection is cu t to the clipb o ard . Fires b efo re a selection is co p ied to th e clipb o ard . Fires b efo re a selection is p asted fro m th e clip b o ard . Fires w h en a selectio n is c op ied to th e clipb o ard . Fires w h en a selectio n is c u t to th e clip b o ard . Fires if im ag e tran sfer h as b een in terru p ted b y u ser. Fires w h en a selectio n is p asted fro m th e clip b o ard .
K ey b o a rd E ve nts onhelp onkeydown onkeypress onkeyup m a r q u e e ev en ts onbounce onfinish onstart M o u se ev en ts oncontextmenu ondblclick ondrag ondragend ondragenter ondragleave ondragover ondragstart ondrop onmousedown onmouseup Fig . 1 4 .1 0 D y n a m ic
Ingenirhjskolen i rhus
F ires w h en th e u ser in itia tes h e lp (i.e ., b y p re ssin g th e F 1 k e y). F ires w h en th e u ser p u sh es d o w n a k e y. F ires w h en th e u ser p resses a key. F ires w h en th e u ser en d s a k ey p ress. F ires w hen d ire ctio n. a scro llin g ma r qu e e b o un ces b ack in th e o th er
Fires im m ed iately after a d atabo u nd o b ject h as b een up d ated . Fires b efo re a d a ta so u rce is u pd ated . Fires w h en a da ta so u rce has ch an g ed . Fires w h en n ew d ata fro m a d ata so u rce beco m e availab le. Fires w h en con ten t at a d ata so u rce h as ch an g ed . Fires w h en co m p leted . Fires w h en availab le. Fires w hen fin ish ed . a a tran sfer of d ata fro m th e d ata so u rce h as
F ires w h en th e co n text m en u is sh o w n (rig h t-click ). F ires w h en th e m o u se is d o u b le-c lic ked . F ires d u rin g a m o u se d rag . F ires w h en a m o u se d rag en d s. F ires w h en so m eth in g is d rag ged o n to an a rea. F ires w h en so m eth in g is d rag ged o u t o f an a rea. F ires w h en a d rag is h eld o v er an a rea. F ires w h en a m o u se d rag b eg in s. F ires w h en d u rin g a d rag . a m o u se b u tto n is released o ve r a va lid targ et
Fires if an erro r occu rs w h ile u pd atin g a d ata field . n ew ro w ro w of of d ata d ata fro m fro m th e th e d ata d ata sou rc e so u rce has is ju st
Fires w h en a row o f d ata fro m th e d ata so u rce is d eleted . Fires w h en a row o f d ata fro m th e d ata so u rce is in serted . HTM L e v e n t s.
Slide 35 af 51
Slide 36 af 51
Ingenirhjskolen i rhus
Events
Illustrating Example
http://www.xs4all.nl/~ppk/js/eventexample.html
M iscellaneous Events o naft erpr int o nbef oree ditf ocu s o nbef orep rint o nbef oreu nloa d o ncha nge o nfil terc hang e o nlos ecap ture o npro pert ycha nge o nrea dyst atec han ge o nres et o nres ize o nscr oll o nsel ect o nsel ects tart o nsto p o nunl oad Fig . 14.10 D yn a m ic
Fires im mediately after the docum ent prints. Fires before an element gains focus for editing. Fires before a document is printed. Fires before a document is unloaded (i.e., the w indow was closed or a link was clicked). Fires w hen a new choice is made in a se le ct element, or when a text input is changed and the element loses focus. Fires w hen a filter changes properties or finishes a transition (see Chapter 15, Filters and T ransitions). Fires w hen the relea se Captu re method is invoked. Fires w hen the property of an object is changed. Fires changes. w hen the read yS tate form resets property (i.e., the of user an clicks element an
Fires w hen the size of an object changes (i.e., the user resizes a window or frame). Fires w hen a window or frame is scrolled. Fires when t extar ea ). a text selection begins (applies to i nput or
Fires w hen the object is selected. Fires w hen the user stops loading the object. Fires w hen a page is about to unload. HTM L e v e n t s.
Slide 37 af 51
Ingenirhjskolen i rhus
Slide 38
Ingenirhjskolen i rhus
Many possibilites
Dynamic menues Editing Facilities (see next) Dynamic table sorting Filters & Transitions
Using an ActiveX component only IE 5.5 > Combined with server side = Windows like
Slide 39
Ingenirhjskolen i rhus
Slide 40
Ingenirhjskolen i rhus
10
Open source (Commercial License exists) Easy to implement with ASP,PHP,JSP,.NET Demo at:
http://www.fckeditor.net/demo/
Slide 41
Ingenirhjskolen i rhus
Slide 42
Ingenirhjskolen i rhus
Dynamic Menues
Simple Examples
http://www.w3schools.com/dhtml/dhtml_examples.asp
Commercial Example:
http://www.opencube.com/vim6.0/template1.html
DoJo:
http://www.dojotoolkit.org/demos/fisheye-demo
http://www.w3schools.com/dhtml/tryit.asp?filename=trydhtml_menu_slide2
Slide 43
Ingenirhjskolen i rhus
Slide 44
Ingenirhjskolen i rhus
11
http://www.htmlgoodies.com/legacy/beyond/dhtml/menu2.html
http://www.htmlgoodies.com/legacy/beyond/dhtml/dhtml5.html
Slide 45 Ingenirhjskolen i rhus Slide 46 Ingenirhjskolen i rhus
Firebug
Slide 47
Ingenirhjskolen i rhus
Slide 48
Ingenirhjskolen i rhus
12
Ajax Overview
Slide 49
Ingenirhjskolen i rhus
Slide 50
Ingenirhjskolen i rhus
AJAX Support
The XMLHttpRequest object supported in
Internet Explorer 5.0+, Safari 1.2, Mozilla 1.0 / Firefox, Opera 8+, Netscape 7
Cons:
Breaks HTTP convention higher complexity browser navigation Overly Complex OR magic components / toolkits Relies heavy on DHTML/JS problems with some browsers Search Engine integration (as in frames) is problematic Browser Navigation problematic
Slide 51
Ingenirhjskolen i rhus
Slide 52
Ingenirhjskolen i rhus
13
AJAX Frameworks
Simple AJAX:
http://www.w3schools.com/ajax/ajax_example.asp http://userportal.iha.dk/~sw/index_with_http_request.htm (HttpRequestObject) http://userportal.iha.dk/~sw/res/external.js
14