Archive for the ‘javascript’ Category

getElementById – javascript取得html的控制

June 18, 2007
noted from 

document.getElementById('navigation').getElementsByTagName('a')[3];	returns the fourth link inside the element with the ID 'navigation'

document.getElementsByTagName('div')[2].getElementsByTagName('p')[0];	returns the first paragraph inside the third div in the document.
childNodes
returns an array of all the nodes inside the current one. There is also
firstChild and lastChild, which are shorter versions
for childNodes[0] and childNodes[this.childNodes.length-1].
parentNode
The element containing this one
nextSibling
the next element on the same level in the document tree
previousSibling
the previous element on the same level in the document tree

Attributes

attributes
returns an array of all the attributes of this element. Does not
work with Internet Explorer below version 6.
data
returns or sets the textual data of the node
nodeName
returns the name of the node (the HTML element name)
nodeType
returns the type of the node — 1 is an element node, 2 attribute and 3 text.
nodeValue
returns or sets the value of the node. This value is the text when
the node is a textnode, the attribute if it is an attribute or null if it
is an element.
getAttribute(attribute)
returns the value of the attribute attribute.
Length
returns the length of array
e.g.
imgs=document.getElementsByTagName('img'); for(i=0;i<imgs.length;i++)

Example

function findimg(){ var image; image=document.getElementById('home'); if (image)   {   image.style.border='3px dashed #ccc';  }}

Find file type

 ftype = IMGID.src.substring(src.lastIndexOf('.'), src.length);

Browser targeted Cascading Style Sheets using JavaScript

June 18, 2007

Noted from the complete webmaster

p.s. CSS are only supported in versions of Netscape 4 and IE 4

<script language=”JavaScript><!–
browser_version= parseInt(navigator.appVersion);
browser_type = navigator.appName;

if (browser_type == “Microsoft Internet Explorer” && (browser_version
>= 4)) {
document.write(“<link REL=’stylesheet’ HREF=’ie.css’
TYPE=’text/css’>”);
}

else if (browser_type == “Netscape” && (browser_version >= 4)) {
document.write(“<link REL=’stylesheet’ HREF=’netscape.css’
TYPE=’text/css’>”);
}

// –></script>