Quick Search:
CODE
Oracle PL/SQL Code Library
JOBS
Find Or Post Oracle Jobs
FORUM
Oracle Discussion & Chat
 JavaScript: Auto-Link words Jump to:  
Category: >> JavaScript >> Auto-Link words Bookmark and Share

<< lastnext >>

Snippet Name: Auto-Link words

Description: Automatically turns every instance of a word into a link.

While functionality like this is best done on the server side, there might also be a need for a JavaScript solution, so here goes:

Comment: (none)

Language: JAVASCRIPT
Highlight Mode: JAVASCRIPT
Last Modified: February 27th, 2009

/* 
  linkWord(obj)
  written by Christian Heilmann (http://wait-till-i.com)
  automatically turns words defined in the object obj into 
  hyperlink. 
  The object obj sent as a paramater needs to contain all the words
  properties and the associated URLs as values.
  Demo object: 
  {
    'wait-till-i.com':'http://wait-till-i.com',
    'icant.co.uk':'http://icant.co.uk'
  }
*/
 
FUNCTION linkWord(obj){
  FOR(i IN obj){
    VAR x = document.body.innerHTML;
    VAR linkStart = '<a href="'+obj[i]+'">';     
    VAR linkEnd = '</a>';     
    VAR reg = NEW RegExp ('\\b' + i + '\\b','g');
    x = x.replace(reg, linkStart + i + linkEnd);
    document.body.innerHTML = x;
  }
}
 
Demo output follows
 
Settings:
 
{
  'example':'http://www.example.com',
  'another':'http://icant.co.uk'
}


Free
Oracle Magazine
Subscriptions
and Oracle White Papers


SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
 
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 94 users online    © 2009 psoug.org

PSOUG LOGIN
Username: 
Password: 
Forgot your password?