Jump to content

Getting this Javascript to work


pioneerx01

Recommended Posts

I am trying to make all non hyper-linked words "cables" as a link to a site without recoding the text. Someone recommended this script to me. I have looked at it as thoroughly as I know how, but I can not get it working. I know nothing about Javascript, which is probably why I can not get it to work.

 

<html>
<head>
<script type="text/javascript">
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;
  }
}
</script>
</head>
<body>
<p>Check out these cables!</p>
</body>
</html>

 

 

 

Where do I place:

 

'cables':'http://www.google.com',

 

to make cables as hyperlink to a site (Google in this example)?

Thanks

Link to comment
Share on other sites

Something, somewhere on your page, looks something like this:

 

<script language="JavaScript">
//something here.
linkWord(obj);
</script>

The "//something here" will be a number of lines that built the object USED by this function.  All I can see is a function that uses an external list of items.

Link to comment
Share on other sites

linkWord(['cable':'http://google.com']);

 

that should do it :)

That should do it, yes, but there's some place where a call like this already exists.  You can simply call this function again like this, or find the right place to add this code and do it there.

 

Not that this solution is wrong, it's perfectly acceptable, but one day you're going to be wondering why the word "cable" still links to google even after you find and remove the large block of linking code.

 

-Dan

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.