AllMyScripts logo - first part
Mortgage Calculator | Loan Calculator | Color Lines
AllMyScripts logo - second part
AllMyScripts logo - third part

Javascript for creating dynamic links

Script Features

The script can be used to link all specifically marked phrases on HTML page. It works this way - you include the script on your page and provide an array that maps each phrase to corresponding link. As soon as a user loads HTML page, the script looks for all phrases wrapped into DIV or SPAN tags with a class "_link", and links the phrases according to the map. To majority of users, who have javascript turned on, this will look like normal page with links. The links on the page won't be visible for users that have javascript disabled in their browsers, search engine bots and spam bots. This exactly why you may want to use this script - to hide links from search engines. There are a few reasons why you would want to do this:

You may also use the script to hide e-mail addresses from spam bots, that is you can generate mailto: links dynamically. It's especially useful if you have multiple e-mail addresses on the page.

Example

Second link Fourth link

How to use it:

  1. Download a file gs_dynlink.js (see links below) - this is the only javascript file that you will need. Put it somewhere on your web server. Do not link to original file on this server because:
  2. Wrap all phrases that you want to link into <DIV> or <SPAN> tags, and add a class "_link" to these tags:
    <div class="_link">First link<div>
    <span class="_link">Second link</span>
    If the phrase inside of the DIV or SPAN tag is too long then it's better to "tag" the link. You can do this by adding " _<tag>" text after the _link class name:
    <span class="_link _3">Third link</span>
    <span class="_link _4">Fourth link</span>
    Please note that the script removes preceding "_" character from the tag text, so that actual tags in the examples above are 3 and 4!

    mailto: links are created in similar way:

    <div class="_link _5">Mail me</div>
  3. Add the following javascript code to the page:
    <script type="text/javascript"><!--
    var L_urlPrefix = '';
    var L_allLinks = new Array (
            'First link', 'http://www.allmyscripts.com/index1.html',
            'Second link', 'http://www.allmyscripts.com/index2.html',
            '3', 'http://www.allmyscripts.com/index3.html',
            '4', 'http://www.allmyscripts.com/index4.html',
            '5', 'mailto:me@allmyscripts.com'
    );
    // -->
    </script>
    <script type="text/javascript" src="/Dynamic_Links/gs_dynlinks.js"></script>
    
    Replace all data inside of Array () with actual phrases/tags and links. The first element of this array should be the first phrase text or a tag, the second element is a link associated with the first phrase/tag, the third element is the second phrase text/tag, the fourth element is the link associated with the second phrase/tag, and so on. The purpose of the L_urlPrefix variable is to store part of URL that will prefix all links in L_allLinks array. If all of your links point to the same website then you can put website name into L_urlPrefix variable, and put only relative links into L_allLinks array. For example, if you don't need "Mail me" link then the javascript code will look like this:
    <script type="text/javascript"><!--
    var L_urlPrefix = 'http://www.allmyscripts.com/';
    var L_allLinks = new Array (
            'First link', 'index1.html',
            'Second link', 'index2.html',
            '3', 'index3.html',
            '4', 'index4.html',
    );
    // -->
    </script>
    <script type="text/javascript" src="/Dynamic_Links/gs_dynlinks.js"></script>
    

Advanced features

Changes from version 1.0

You can specify which tags will be searched by the script by setting L_searchTags variable. Fixed script initialization code.

Version 1.1 of the script is directly compatible with version 1.0. You can replace old version of the script with the new version without changing your HTML pages.

Download:

gs_dynlinks.js v1.1 (latest version) - uncompressed file
gs_dynlinks.1.0.js v1.0 - uncompressed file

License:

The script is released under GPL v3.0 license. You're welcome to distribute the script and to use it on your web pages if you like. If you decide to modify it please make sure that you release the changed code.

Contact

If you have any questions, corrections or additions to the code, you can contact me using this form.

Spread the word:

If you use this script and like it please consider adding a link to this page. This will make it easier for other people to find this script.

(c) Copyright 2007 Gennadiy Shvets