Jump to content

A Quick Question


Chocolate570

Recommended Posts

Ok. Like I said, I feel really stupid. Really, really stupid. Ok, so I need a script that changes all ": )"'s into smiley images (without the space between : and ).) So far I have this:

<html><body><div id="txt">Ok, I like monkeys. :)Monkeys are cool. :)</div><script type="text/javascript">x=document.getElementById("txt");zoop='<img src="http://www.smileygenerator.us/community/html/emoticons/smile.gif" />'lol=x.innerHTML.replace(":)",zoop);x.innerHTML=lol;</script>

But unfortunatley, this script only changes the FIRST instance of : ), but not the rest. What's with that? How can I mae this work? I was about to use a for loop through every character of the innerHTML and then check that character and the one next to it together to see if they are : and ), but why do that much code when I know there's a simple solution? Thanks.Choco

Link to comment
Share on other sites

I tried the code above, but it didn't work. : and ) are probably special characters. So I backspaced them and it worked beautifully.lol=x.innerHTML.replace(/\:\)/g,zoop);Thanks so much for that page, and the code. :)Choco

Link to comment
Share on other sites

Ok, again. What's wrong with this code?

//Start Replacement Scripts.//bodyHlder=document.body;imgRepfir='<img src="http://www.planetsmilies.com/smilies/happy/happy0069.gif" />'finRepf=bodyHlder.innerHTML.replace(/\:\)/g,imgRepfir);bodyHlder.innerHTML=finRepf;imgRepsec='<img src="http://www.planetsmilies.com/smilies/confused/confused0069.gif" />'finRepg=bodyHlder.innerHTML.replace(/\:\S/g,imgRepsec);bodyHlder.innerHTML=finRepg;bodyHlder=document.body;imgRepthi='<img src="http://www.planetsmilies.com/smilies/sad/sad0119.gif" />'finReph=bodyHlder.innerHTML.replace(/\:\(/g,imgRepthi);bodyHlder.innerHTML=finReph;bodyHlder=document.body;imgRepfo='<img src="http://www.planetsmilies.com/smilies/tongue/tongue0021.gif" />'finRepj=bodyHlder.innerHTML.replace(/\:\P/g,imgRepfo);bodyHlder.innerHTML=finRepj;bodyHlder=document.body;imgRepfi='<img src="http://www.planetsmilies.com/smilies/winking/winking0001.gif" />'finRepk=bodyHlder.innerHTML.replace(/\;\)/g,imgRepfi);bodyHlder.innerHTML=finRepk;///End Replacement Scripts///

Link to comment
Share on other sites

It's not the problem, but it's not a good solution to use. Think about it, document.body.innerHTML.replace(/</g,"") destroys the entire DOM tree in an instant, and you'll probably end up with an operation aborted or similar. Also, the script you use will probably end up inside the body of the page 60% of the time, so any innerHTML replace of an exact text in the body also replaces it in the script. You can see where the errors come in...

Link to comment
Share on other sites

What exactly is happening? The only thing I can see is that you put a \ in front of S and P. These aren't special characters, so I don't see why you would need the \.Here is the code without the \ in front of the S and P:

//Start Replacement Scripts.//bodyHlder=document.body;imgRepfir='<img src="http://www.planetsmilies.com/smilies/happy/happy0069.gif" />'finRepf=bodyHlder.innerHTML.replace(/\:\)/g,imgRepfir);bodyHlder.innerHTML=finRepf;imgRepsec='<img src="http://www.planetsmilies.com/smilies/confused/confused0069.gif" />'finRepg=bodyHlder.innerHTML.replace(/\:S/g,imgRepsec);bodyHlder.innerHTML=finRepg;bodyHlder=document.body;imgRepthi='<img src="http://www.planetsmilies.com/smilies/sad/sad0119.gif" />'finReph=bodyHlder.innerHTML.replace(/\:\(/g,imgRepthi);bodyHlder.innerHTML=finReph;bodyHlder=document.body;imgRepfo='<img src="http://www.planetsmilies.com/smilies/tongue/tongue0021.gif" />'finRepj=bodyHlder.innerHTML.replace(/\:P/g,imgRepfo);bodyHlder.innerHTML=finRepj;bodyHlder=document.body;imgRepfi='<img src="http://www.planetsmilies.com/smilies/winking/winking0001.gif" />'finRepk=bodyHlder.innerHTML.replace(/\;\)/g,imgRepfi);bodyHlder.innerHTML=finRepk;///End Replacement Scripts///

Let me know if that works.

Link to comment
Share on other sites

Hey,Thanks for that. I changed a couple of things in your script (like the document.body) and it still doesn't work. :S

//Start Replacement Scripts.//bodyHlder=document.getElementById("page");imgRepfir='<img src="http://www.planetsmilies.com/smilies/happy/happy0069.gif" />'finRepf=bodyHlder.innerHTML.replace(/\:\)/g,imgRepfir);bodyHlder.innerHTML=finRepf;imgRepsec='<img src="http://www.planetsmilies.com/smilies/confused/confused0069.gif" />'finRepg=bodyHlder.innerHTML.replace(/\:S/g,imgRepsec);bodyHlder.innerHTML=finRepg;imgRepthi='<img src="http://www.planetsmilies.com/smilies/sad/sad0119.gif" />'finReph=bodyHlder.innerHTML.replace(/\:\(/g,imgRepthi);bodyHlder.innerHTML=finReph;imgRepfo='<img src="http://www.planetsmilies.com/smilies/tongue/tongue0021.gif" />'finRepj=bodyHlder.innerHTML.replace(/\:P/g,imgRepfo);bodyHlder.innerHTML=finRepj;imgRepfi='<img src="http://www.planetsmilies.com/smilies/winking/winking0001.gif" />'finRepk=bodyHlder.innerHTML.replace(/\;\)/g,imgRepfi);bodyHlder.innerHTML=finRepk;///End Replacement Scripts///

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...