Archive for November, 2007

Web server certificate - 272Part IIIDocument Objects Referencebeginning. Browsers prior to IE4

Friday, November 23rd, 2007

272Part IIIDocument Objects Referencebeginning. Browsers prior to IE4 and NN6 limited these methods primarily to the windowobject and form control elements. WindowsFor window objects, the blur()method (NN3+, IE4+) pushes the referenced window to theback of all other open windows. If other browser suite windows (such as e-mail or newsreader windows) are open, the window receiving the blur()method is placed behind thesewindows as well. The window.blur()method does not adjust the stacking order of the current window inMozilla-based browsers (thus the Put Me in Back button in Listing 15-23 doesn t work inthose browsers). But a script in a window can invoke the focus()method of another windowto bring that other window to the front (provided a scriptable linkage, such as the window. openerproperty, exists between the two windows). The minute you create another window for a user in your Web site environment, you mustpay attention to window layer management. With browser windows so easily activated by theslightest mouse click, a user can lose a smaller window behind a larger one in a snap. Mostinexperienced users don t think to check the Windows taskbar or browser menu bar (if thebrowser is so equipped) to see if a smaller window is still open and then activate it. If thatsubwindow is important to your site design, then you should present a button or otherdevice in each window that enables users to safely switch among windows. The window. focus()method brings the referenced window to the front of all the windows. Rather than supply a separate button on your page to bring a hidden window forward, youshould build your window-opening functions in such a way that if the window is alreadyopen, the function automatically brings that window forward (as shown in Listing 15-23). Thisremoves the burden of window management from your visitors. The key to success with this method is making sure that your references to the desired win- dows are correct. Therefore, be prepared to use the window.openerproperty to refer to themain window if a subwindow needs to bring the main window back into focus. Form control elementsThe blur()and focus()methods apply primarily to text-oriented form controls: text input, select, and textareaelements. Just as a camera lens blurs when it goes out of focus, a text object blurs when it losesfocus when someone clicks or tabs out of the field. Under script control, blur()deselectswhatever may be selected in the field, and the text insertion pointer leaves the field. Thepointer does not proceed to the next field in tabbing order, as it does if you perform a blur bytabbing out of the field manually. For a text object, having focus means that the text insertion pointer is flashing in that textobject s field. Giving a field focus is like opening it up for human editing. Setting the focus of a text field or textareadoes not, by itself, enable you to place the cursorat any specified location in the field. The cursor usually appears at the beginning of the text. To prepare a field for entry to remove the existing text, use both the focus()and select() methods in series. There is a caveat about using focus()and select()together to preselect the content of atext field for immediate editing: Many versions of Internet Explorer fail to achieve the desiredresults due to an internal timing problem. You can work around this problem (and remainCautionelementObject.blur()

271Chapter 15Generic HTML Element ObjectsThe version with attachEvent()is (Web hosting bandwidth)

Friday, November 23rd, 2007

271Chapter 15Generic HTML Element ObjectsThe version with attachEvent()is as follows: myObject.attachEvent( onmousedown , setHilite); Both parameters are required. The first parameter is a string version (case-insensitive) of theevent name. The second is a reference to the function to be invoked when the event fires forthis object. A function referenceis an unquoted, case-sensitive identifier for the function withoutany parentheses (which also means that you cannot pass parameters in this function call). There is a subtle benefit to using attachEvent()over the event property binding approach. When you use attachEvent(), the method returns a Boolean value of trueif the event bind- ing succeeds. IE triggers a script error if the function reference fails, so don t rely on areturned value of falseto catch these kinds of errors. Also, there is no validation that theobject recognizes the event name. If you have used attachEvent()to bind an event handler to an object s event, you can dis- connect that binding with the detachEvent()method. The parameters are the same as forattachEvent(). The detachEvent()method cannot unbind events whose associations areestablished via tag attributes or event property settings. The W3C DOM event model provides functionality similar to these IE-only methods: addEventListener()and removeEventListener(). ExampleUse The Evaluator (Chapter 13) to create an anonymous function that is called in response toan onmousedownevent of the first paragraph on the page. Begin by assigning the anonymousfunction to global variable a(already initialized in The Evaluator) in the upper text box: a = new Function( alert( Function created at + (new Date()) + ) ) The quote marks and parentheses can get jumbled easily, so enter this expression carefully. When you enter the expression successfully, the Results box shows the function s text. Nowassign this function to the onmousedownevent of the myPelement by entering the followingstatement into the upper text box: document.getElementById( myP ).attachEvent( onmousedown , a) The Results box displays truewhen successful. If you mouse down on the first paragraph, analert box displays the date and time that the anonymous function was created (when the newDate()expression was evaluated). Now, disconnect the event relationship from the object by entering the following statementinto the upper text box: document.getElementById( myP ).detachEvent( onmousedown , a) Related Items:addEventListener(), detachEvent(), dispatchEvent(), fireEvent(), removeEventListener()methods; Event binding (Chapter 14). blur() focus() Returns:Nothing. Compatibility:WinIE3+, MacIE3+, NN2+, Moz1+, Safari1+ The blur()method removes focus from an element, while the focus()method gives focusto an element. Even though the blur()and focus()methods have been around since theearliest scriptable browsers, not every focusable object has enjoyed these methods since theelementObject.blur()

270Part IIIDocument Objects ReferenceListing 15-22(continued) function applyOutside() { (Web hosting mysql)

Thursday, November 22nd, 2007

270Part IIIDocument Objects ReferenceListing 15-22(continued)

applyElement() Method



A simple paragraph with a special word in it.


The visible results of applying the emelement inside and outside the spanelement in thiscase are the same. But you can see from the HTML results that each element impacts the ele- ment hierarchy quite differently. Related Items:insertBefore(), appendChild(), insertAdjacentElement()methods. attachEvent( eventName , functionRef) detachEvent( eventName , functionRef) Returns:Boolean. Compatibility:WinIE5+, MacIE-, NN-, Moz-, Safari- The attachEvent()method is used primarily within code that specifies IE behaviors (seeChapter 48 on the CD-ROM). But you can also use it in regular scripting as yet another way tobind an event handler to an object. The following example characterizes the more typicalapproach to assigning an event handler: myObject.onmousedown = setHilite; elementObject.applyElement()

Web site designers - 269Chapter 15Generic HTML Element ObjectsThe final part of

Wednesday, November 21st, 2007

269Chapter 15Generic HTML Element ObjectsThe final part of the demonstration uses the removeChild()method to peel away all children ofthe ulelement until just the two original items are left standing. Again, the lastChildpropertycomes in handy as the restore()function keeps removing the last child until only two remain. Related Items:removeChild(), replaceChild()methods; nodes and children (Chapter 14). applyElement(elementObject[, type]) Returns:Nothing. Compatibility:WinIE5+, MacIE-, NN-, Moz-, Safari- The applyElement()method enables you to insert a new element as the parent or child of thecurrent object. An important feature of this method is that the new object is wrapped around thecurrent object (if the new element is to become the parent) or the current object s content (if thenew element is to become a child). When the new element becomes a child, all previous childrenare nested further by one generation to become immediate children of the new element. You canimagine how the resulting action of this method affects the containment hierarchy of the currentelement, so you must be careful in how you use the applyElement()method. One parameter, a reference to the object to be applied, is required. This object may be gener- ated from constructions such as document.createElement()or from one of the child ornode methods that returns an object. The second parameter is optional, and it must be one ofthe following values: Parameter ValueDescriptionOutsideNew element becomes the parent of the current objectInsideNew element becomes the immediate child of the current objectIf you omit the second parameter, the default value (outside) is assumed. Listing 15-22shows how the applyElement()method is used both with and without default values. ExampleTo help you visualize the impact of the applyElement()method with its different parametersettings, Listing 15-22 enables you to apply a new element (an emelement) to a spanelementinside a paragraph. At any time, you can view the HTML of the entire pelement to see wherethe emelement is applied as well as its impact on the element containment hierarchy for theparagraph. After you load the page, inspect the HTML for the paragraph before doing anything else. Notice the spanelement and its nested fontelement, both of which surround the one-wordcontent. If you apply the emelement inside the spanelement (click the middle button), thespanelement s first (and only) child element becomes the emelement; the fontelement isnow a child of the new emelement. Listing 15-22: Using the applyElement() Method ContinuedelementObject.applyElement()

268Part IIIDocument Objects ReferenceInvoking the replaceChild()method in the (Domain and web hosting)

Wednesday, November 21st, 2007

268Part IIIDocument Objects ReferenceInvoking the replaceChild()method in the replace()function utilizes some of the samecode. The main difference is that the replaceChild()method requires a second parameter: a reference to the child element to be replaced. This demonstration replaces the final childnode of the ullist, so the function takes advantage of the lastChildproperty of all elementsto get a reference to that final nested child. That reference becomes the second parameter toreplaceChild(). Listing 15-21: Various Child Methods

Child Methods


Here is a list of items:
  • First Item
  • Second Item
Enter some text to add/replace in the list:

elementObject.appendChild()

267Chapter 15Generic HTML Element Objects Capture -type click listener: (X web hosting)

Tuesday, November 20th, 2007

267Chapter 15Generic HTML Element Objects Capture -type click listener:

Related Items:attachEvent(), detachEvent(), dispatchEvent(), fireEvent(), removeEventListener()methods. appendChild(elementObject) Returns:Node object reference. Compatibility:WinIE5+, MacIE5+, NN6+, Moz1+, Safari1+ The appendChild()method inserts an element or text node (defined by other code that comesbefore it) as the new, last child of the current element. Aside from the more obvious applicationof adding a new child element to the end of a sequence of child nodes, the appendChild() method is also practical for building element objects and their content before appending, replac- ing, or inserting the element into an existing document. The document.createElement() method generates a reference to an element of whatever tag name you assign as that method sparameter. The appendChild()method returns a reference to the appended node object. This referencediffers from the object that is passed as the method s parameter because the returned valuerepresents the object as part of the document rather than as a freestanding object in memory. ExampleListing 15-21 contains an example that shows how to use the appendChild()method in con- cert with removeChild()and replaceChild()to modify child elements in a document. Because many W3C DOM browsers treat source code carriage returns as text nodes (and thuschild nodes of their parent), the HTML for the affected elements in Listing 15-21 is shownwithout carriage returns between elements. The append()function creates a new lielement and then uses the appendChild()method toattach the text box text as the displayed text for the item. The nested expression, document. createTextNode(form.input.value), evaluates to a legitimate node that is appended tothe new liitem. All of this occurs before the new liitem is added to the document. In thefinal statement of the function, appendChild()is invoked from the vantage point of the ulelement thus adding the lielement as a child node of the ulelement. elementObject.appendChild()

266Part IIIDocument Objects ReferenceListing 15-20(continued) // (Web hosting uk) add event

Monday, November 19th, 2007

266Part IIIDocument Objects ReferenceListing 15-20(continued)

W3C Event Model Lab



This paragraph (a SPAN element nested inside a P element) can be set to listen for click events.



Examine click event characteristics: 

elementObject.addEventListener()

265Chapter 15Generic HTML Element Objectscall. Therefore, functions that (Business web hosting)

Monday, November 19th, 2007

265Chapter 15Generic HTML Element Objectscall. Therefore, functions that need to reference forms or form control elements must buildtheir own references (with the help of the event object s property that says which object isthe event s target). By default, the W3C DOM event model has events bubble upward through the element con- tainer hierarchy starting with the target object of the event (for example, the button beingclicked). However, if you specify truefor the third parameter of the addEventListener() method, event capture is enabled for this particular event type whenever the current objectis the event target. This means that any other event type targeted at the current object bub- bles upward unless it, too, has an event listener associated with the object and the thirdparameter is set to true. Using the addEventListener()method requires that the object to which it is attachedalready exists. Therefore, you most likely will use the method inside an initialization functiontriggered by the onloadevent handler for the page. (The documentobject can useaddEventListener()for the load event immediately because the documentobject existsearly in the loading process.) A script can also eliminate an event listener that was previously added by script. TheremoveEventListener()method takes the same parameters as addEventListener(), which means that you can turn off one listener without disturbing others. In fact, becauseyou can add two listeners for the same event and listener function (one set to capture andone not a rare occurrence, indeed), the three parameters of the removeEventListener() enable you to specify precisely which listener to remove from an object. Unlike the event capture mechanism of NN4, the W3C DOM event model does not have a global capture mechanism for an event type regardless of target. And with respect toInternet Explorer, the addEventListener()method is closely analogous to the IE5+ attachEvent()method. Also, event capture in IE5+ is enabled via the separatesetCapture()method. Both the W3C and IE event models use their own syntaxes to bindobjects to event handling functions, so the actual functions may be capable of serving bothmodels with browser version branching required only for event binding. See Chapter 25 formore about event handling with these two event models. ExampleListing 15-20 provides a compact workbench to explore and experiment with the basic W3CDOM event model. When the page loads, no event listeners are registered with the browser(except for the control buttons, of course). But you can add an event listener for a clickevent in bubble and/or capture mode to the bodyelement or the pelement that surroundsthe spanholding the line of text. If you add an event listener and click the text, you see areadout of the element processing the event and information indicating whether the eventphase is bubbling (3) or capture (1). With all event listeners engaged, notice the sequence ofevents being processed. Remove listeners one at a time to see the effect on event processing. Listing 15-20: W3C Event Lab ContinuedelementObject.addEventListener()

264Part IIIDocument Objects ReferenceRelated Items:readyStateproperty; removeBehavior()method; behaviors (Chapter (Make web site)

Sunday, November 18th, 2007

264Part IIIDocument Objects ReferenceRelated Items:readyStateproperty; removeBehavior()method; behaviors (Chapter 48 onthe CD-ROM). addEventListener( eventType , listenerFunc, useCapture) removeEventListener( eventType , listenerFunc, useCapture) Returns:Nothing. Compatibility:WinIE-, MacIE-, NN6+, Moz1+, Safari1+ The W3C DOM s event mechanism accommodates both event bubbling and trickling (seeChapter 25). While the new mechanism supports the long-standing notion of binding an eventto an element by way of HTML attributes (for example, the old onclickevent handler), itencourages binding events by registering an event listener with an element. (In browsers thatsupport the W3C event model, other ways of binding events such as event handlerattributes are internally converted to registered events.) To tell the DOM that an element should listen for a particular kind of event, use theaddEventListener()method on the element object. The method requires three parameters. The first is a string version of the event type for which the element should listen. Event typestrings do not include the well-used on prefix of event handlers. Instead, the names consistonly of the event and are usually in all lowercase (except for some special system-wide eventspreceded by DOM). Table 15-7 shows all the events recognized by the W3C DOM specification(including some new DOM ones that are not yet implemented in browsers). Table 15-7: W3C DOM Event Listener Typesaborterrorblurfocuschangeloadclickmousedown DOMActivatemousemove DOMAttrModifiedmouseoutDOMCharacterDataModifiedmouseoverDOMFocusIn mouseupDOMFocusOut resetDOMNodeInsertedresizeDOMNodeInsertedIntoDocumentscrollDOMNodeRemovedselectDOMNodeRemovedFromDocumentsubmitDOMSubtreeModifidunloadNote that the event types specified in the DOM Level 2 are more limited than the wide rangeof events defined in IE4+. Also, the W3C temporarily tabled the issue of keyboard events untilDOM Level 3. Fortunately, most W3C-compatible browsers implement keyboard events in afashion that likely will appear as part of the W3C DOM Level 3. The second parameter of the addEventListener()method is a reference to the JavaScriptfunction to be invoked. This is the same form used to assign a function to an event propertyof an object (for example, objReference.onclick = someFunction), and it should notbe aquoted string. This approach also means that you cannot specify parameters in the functionelementObject.addEventListener()

263Chapter 15Generic HTML Element (Web hosting domains) Objects} } function turnOff()

Saturday, November 17th, 2007

263Chapter 15Generic HTML Element Objects} } function turnOff() { document.getElementById( myP ).removeBehavior(myPBehaviorID); } function setColor(select, color) { if (document.getElementById( myP ).hotColor) { document.getElementById( myP ).setHotColor(color); } else { alert( This feature is not available. Turn on the Behavior first. ); select.selectedIndex = 0; } } function showBehaviorCount() { var num = document.getElementById( myP ).behaviorUrns.length; var msg = The myP element has + num + behavior(s). ; if (num > 0) { msg += Name(s): rn ; for (var i = 0; i < num; i++) { msg += document.getElementById( myP ).behaviorUrns[i] + rn ; } } alert(msg); }

addBehavior() and removeBehavior() Method Lab



This is a sample paragraph. After turning on the behavior, it will turn your selected color when you mouse down anywhere in thisparagraph.

Choose a hot color:

To turn off the behavior, the removeBehavior()method is invoked. Notice that theremoveBehavior()method is associated with the myPobject, and the parameter is the IDof the behavior added earlier. If you associate multiple behaviors with an object, you canremove one without disturbing the others because each has its own unique ID. elementObject.addBehavior()


Control Panel
Bubble -type click listener: