free hosting   image hosting   hosting reseller   online album   e-shop   famous people 
Free Website Templates
Free Installer

Page Link Counter



This script lets you count the number of hyperlinks on a page.

I've used it as a script count on the Cyberschool Tips page.

All links there are for scripts only, so the script count is incremented automatically whenever the page is updated.


Paste the code below in the body of your page.


NOTE:

The body section code must be after all links to be counted.
If the code is inserted above your links, the total count will be zero.

How It Works

The actual link count is performed by the statement:
document.links.length
where:
  • document is the page itself
  • links is the link property of that page
  • length is the number of links on the page
  • This value is assigned to the variable max.
    Next, the link information is displayed using a delayed alert box:
    setTimeout("alert('Total Links = ' + max)",3000)

    Note the use of double and single quotes in the above setTimeout statement.
  • The double quotes refer to the parameters of the setTimeout.
  • The single quotes refer to the message content of the alert box.

  • Instead of using an alert box, you could print the result to your page using the following:

    Writing the Data Above The Links

    The above examples can only write the link count data below the links on the page.

    To write the information to a location above the links, here are 2 methods:


    1. Text Input Box

    This section goes where you wish above your links:
    And this code goes beneath all links:
    The link count value max is printed to the text input box using:
    document.total.linkz.value = " " + max
    where:
  • document is the page itself
  • total is the ID of the master form
  • linkz is the ID for the text input box inside that form
  • value is the full text string including the link count
  • The 2 blank spaces between the quotes are used for a nicer cosmetic appearance.


    2. DIV Placeholder

    This section goes wherever you like above your links:
    This section goes beneath all links:
    A DIV placeholder with the ID linkcounter is located as desired above the links.

    The variable str stores the line to be printed:
    var str = "Total Number of Links: " + max
    This is printed to the DIV placeholder using:
    linkcounter.innerHTML = str
    With this method, the innerHTML command is used to perform the writing.

    That's because the DIV object is user-defined, and is not a standard element of the document hierarchy.


    Try It Out

    All the above methods of writing the link count are used in the example below:
    Open Link Count Test Page



    © 2001 CyberSchool