Play with DOM in Javascript

Posted by JDK | 12:18 PM | | 0 comments »

Very Important points in Advanced Javascript

Q: How to find no. of DIV tags in a HTML file through Javascript

Suppose you need to know how many DIV elements there are on the page. In Netscape Navigator, the number of layers is document.layers.length. It is not that trivial in Internet Explorer.

You need to use the tags method of the document.all array. The tags() method retrieves all HTML elements of a particular tag from the document.all array. It returns an array of elements. The following line will populate the given array with all DIV elements:

var divArray = document.all.tags("DIV");

To find the total number of DIV elements, just use divArrary.length. For example, click this link from within Internet Explorer to compute and echo the number of FONT elements in this tip. We just put the following line in the JavaScript section at the top of this page:

var divArray = document.all.tags("FONT");

And the link above is assembled as:

tip is ' + divArray.length)">compute and echo

Q: How to collect and display no. of drives in your system

Suppose you want to do some work with your PC drives. A good starting point will be the drives collection. It holds a read-only collection of all available drives. A collection is not an array. It is much more difficult to iterate through a collection. You can do it only by using the Enumerator object. The Enumerator object supports the following methods: moveFirst(), moveNext(), item(), and atEnd(). The drives collection supports two properties by its own: count and item. The following example demonstrates both drives collection's properties as well as the Enumarator's methods:

The enumerator variable used above is coll. Be careful not to use the word enum, as it is apparently reserved but not documented.

Q: How to do folder operations through Javascript

You create an ActiveX File System object by calling ActiveXObject() with a single argument, Scripting.FileSystemObject:

myActiveXObject = new ActiveXObject("Scripting.FileSystemObject");

You create a folder object by using the ActiveX's GetFolder() method:

myFolder = myActiveXObject.GetFolder("c:\\temp");

Here are the Folder object's properties:

Property

Description

Attributes

Refers to the folder attributes

DateCreated

Returns the folder's creation date

DateLastAccessed

Returns the folder's last-accessed date

DateLastModified

Returns the folder's last-modified date

Drive

Returns the folder's letter drive

Files

Returns the folder's files collection, containing all the file objects in the folder

IsRootFolder

Returns true if the folder is the root folder, false otherwise

Name

Returns the folder name

ParentFolder

Returns the folder's parent folder name

Path

Returns the folder's long path

ShortName

Returns the folder's short name

ShortPath

Returns the folder's short path

Size

Returns the folder size

SubFolder

Returns a folders collection of all folders contained in the folder

Type

Returns the folder type

Here are the Folder object's methods:

Method

Description

Copy()

Copies the folder from its parent folder or the root to another folder or the root

Delete()

Removes the folder

Move()

Moves the folder from its parent folder or the root to another folder or the root

0 comments