engateedgeway
New Member
I'm writing a smiley parsing function for my website. What I'm trying to accomplish is to transform certain strings, e.g. \[code\]""\[/code\] into an image like this:
Or here's the actual html as an example:\[code\]"" ===> <img src="http://stackoverflow.com/questions/15721347/images/smilies/smile.png" />\[/code\]My function does what it's meant to do, but it is also parsing native javascript function names! What I mean by this, is if I type a comment containing the strings \[code\]"push"\[/code\] , \[code\]"pop"\[/code\] , or \[code\]"some"\[/code\] (there are probably loads others) my function will parse those strings into invalid images like this:
Here is an html string showing this:\[code\]<img src="http://stackoverflow.com/questions/15721347/images/smilies/function some() { [native code] }" alt="">\[/code\]This results in a 404 not found error in the browser console.\[code\]Failed to load resource: the server responded with a status of 404 (Not Found) \[/code\]Why is this happening? I'm not doing anything too unusual in my code as you can see here:\[code\] function parse_new_comment(commentElem){ $(commentElem).html(parse_comment($(commentElem).text())); } function parse_comment(comment){ var formatted_comment = ""; var smilies = new Array(); smilies[""] = "smile.png"; smilies[""] = "smile-big.png"; smilies[""] = "tongue.png"; smilies["[sheep]"] = "sheep.png"; smilies["<3"] = "love.png"; smilies["[love]"] = "love.png"; var words = comment.split(" "); for (var i = 0; i < words.length; i++) { if(smilies[words] !== undefined){ formatted_comment += ' <img src="http://stackoverflow.com/questions/15721347/images/smilies/'+smilies[words]+'" alt="" />'; }else{ formatted_comment += ' ' + words; } } return formatted_comment; }\[/code\]I have a feeling that this line of the code is causing the problem \[code\]if(smilies[words] !== undefined){\[/code\], as \[code\]push\[/code\] and \[code\]pop\[/code\] are array functions, i'm not too sure though... I would appreciate if someone could suggest any ideas on why my function is failing.Oh I forgot to mention, my page uses ajax to do everything, so new comments are parsed by calling the function like this:\[code\]parse_new_comment($("#comment_343"));\[/code\]Thank you.