Click to See Complete Forum and Search --> : Is It Possible?
DoyleRules
05-13-2003, 08:48 PM
I'm curious, is it possible, with Javascript, to make it automatically scan through a document and turn every instance of a specific word into a link? I realize that this would be a function that would be called out in the onLoad area of the BODY tag. But, could Javascript essentially do something to this matter?
Actually, now that I think of it, one might could do it by simply creating a STYLE, if it was possible to create a style that calls out a link, ...not for sure what the syntax would be for that.
Any help, or ideas would be greatly appreciated. This one's got me puzzled. ....I'm afraid to search for it on a search engine, would probably get tons of other irrelevant mess to sort through.
Thanks,
<--=MikeDoyle=--<<
lmccord2
05-13-2003, 08:55 PM
Yes, it is. I'm not sure of the code exactly, but in some forums, when you do a :-), for example, the script changes it to <img src=smilie.gif width=20 height=20> or something. So it is possible. In fact, it is in a post I read the other day with the same question, only about smilies.
khalidali63
05-13-2003, 09:00 PM
Yes its possible,just have to use DOM methods to modifye the contents..
You could also get the innerHTML of your <body> tag and use a str.replace(regexp, replacement) and a Regular Expression to chage the instance of the word to a link.
And here is how you would do it....
<html>
<head>
<script language="javascript" type="text/javascript">
function replaceText() {
html = document.getElementsByTagName("body")[0].innerHTML;
html = html.replace(/text/g, "<a href=\"http://www.w3c.org\">text</a>");
document.getElementsByTagName("body")[0].innerHTML = html;
}
</script>
</head>
<body onload="replaceText();">
This is some text. And more text.
</body>
</html>
If you want text and Text (case insensativity) add an i right before the g in the regexp (ie /text/ig )
DoyleRules
05-14-2003, 12:20 AM
Oh my dear LORD!! That works!! You are AWESOME pyro!! That is just awesome!
Do you know how I could use that same technique to do the same thing with a phrase, instead of just 1 word. By that I mean, replace a phrase, 3 words, with those same 3 words, just make them a link.
Also, is it possible to use that same technique on a DIV instead of the BODY tag. I would like to apply it to text in a specific DIV, but I have multiple DIV's on this page, and the script affects everything within the BODY tag.
Thanks again,
MD
That kinda of appreciation just made my day... :D
Here's the script with some modifications... It should be what you need...
<html>
<head>
<script language="javascript" type="text/javascript">
function replaceText() {
html = document.getElementById("mydiv").innerHTML;
html = html.replace(/change three words/g, "<a href=\"http://www.w3c.org\">change three words</a>");
document.getElementById("mydiv").innerHTML = html;
}
</script>
</head>
<body onload="replaceText();">
<div id="mydiv">We are going to change three words but don't change this.</div>
<br/>
Don't change three words here...
</body>
</html>
BTW, just to note... this is what Khalid was taking about by using DOM methods to modify the contents....
DoyleRules
05-16-2003, 01:16 PM
Hey Pyro,
again dude, you're the best! I could swear that is exactly what I had tried before I asked you, but you've got the "Midas" touch man!
Ok, here's another addition to this creation. I am trying to make one of these links actually execute a javascript function already called out below the <head>. So instead of this:
html = html.replace(/change three words/g, "<a href=\"http://www.w3c.org\">change three words</a>");
I would like to try something to this effect:
html = html.replace(/change three words/g, "<a onClick=\"chngCont(1); showMe()\">change three words</a>");
I thought this would work, but I get an error. What is your suggestion, what am I doing wrong?
Thanks again Pyro!!
Hmm... I don't know. This here will give you an alert:
html = html.replace(/change three words/g, "<a href=\"http://www.w3c.org\" onclick=\"alert('test'); return false;\">change three words</a>");