pioneerx01 Posted February 18, 2011 Share Posted February 18, 2011 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 More sharing options...
ManiacDan Posted February 18, 2011 Share Posted February 18, 2011 Whatever CALLS this function passes it an object, that's what you need to edit. -Dan Link to comment https://forums.phpfreaks.com/topic/228137-getting-this-javascript-to-work/#findComment-1176501 Share on other sites More sharing options...
pioneerx01 Posted February 19, 2011 Author Share Posted February 19, 2011 I am sorry but I have no idea what that means. An example would be nice Link to comment https://forums.phpfreaks.com/topic/228137-getting-this-javascript-to-work/#findComment-1176579 Share on other sites More sharing options...
ManiacDan Posted February 22, 2011 Share Posted February 22, 2011 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 https://forums.phpfreaks.com/topic/228137-getting-this-javascript-to-work/#findComment-1178191 Share on other sites More sharing options...
RussellReal Posted February 24, 2011 Share Posted February 24, 2011 linkWord(['cable':'http://google.com']); that should do it Link to comment https://forums.phpfreaks.com/topic/228137-getting-this-javascript-to-work/#findComment-1179013 Share on other sites More sharing options...
ManiacDan Posted February 24, 2011 Share Posted February 24, 2011 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 https://forums.phpfreaks.com/topic/228137-getting-this-javascript-to-work/#findComment-1179081 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.