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
https://forums.phpfreaks.com/topic/228137-getting-this-javascript-to-work/
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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.