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:
- To avoid search engine penalties: Search engines don't like pages
that have too many links. Using the script allows you to put as many
links as you want on the page and at the same time avoid possible
penalties.
- To avoid Adsense public ads: Google Adsense may display public ads
on pages that have too many links. You can avoid this by replacing
some or all of the links with dynamically generated links.
- To prevent passing page rank to external pages: Since search
engines won't know about the links they won't pass page rank to sites
linked from the page.
- To link to questionable (from search-engine standpoint) sites:
Google and other search engines may penalize your page or a site for
linking to web spamming sites, or sites in bad neighborhood. If you
use the script to create the links "on the fly" you can avoid
penalties associated with linking to "bad" sites.
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
First link
Second link
Third link
Fourth link
Mail me
How to use it:
- 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:
- You will be using my bandwidth, and I will not like it.
- I may post updated version of the javascript file with the
same name, and this may break your pages.
- If somebody breaks into my server it'd be easy for them to
modify the file and do some nasty things to your page, like
getting user cookies for your domain, or redirecting all users
from your page to some other page.
- 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>
- 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
Placing dynamic links into HTML tags other than DIV and SPAN
You can specify which HTML tags are searched by the script by setting
optional variable L_searchTags:
var L_searchTags = new Array ('TD', 'DIV');
In the example above the script will search for all TD and DIV tags
that have the class '_link' set, and then dynamically link the
contents of all of these tags.
Reducing the size of HTML pages with large lists of dynamic
links.
If you have several HTML pages containing large lists of dynamically generated links,
and, if majority of dynamic links are reused on multiple pages, then
you can reduce the size of all pages by moving definition of
L_allLinks and other variables into external javascript file. Let's
assume that a few HTML pages use L_allLinks definition from step 3 of
"How to use it:" section. Create a file "mylinks.js" that contains:
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'
);
Then replace the javascript code from step 3 with:
<script type="text/javascript" src="mylinks.js"></script>
<script type="text/javascript" src="/Dynamic_Links/gs_dynlinks.js"></script>
If your pages use common dynamic links, as well as page-specific
dynamic links, you can still combine all links into one array and
place it into external javascript file.
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 |