开发者

How to make an HTML character equal to its keyboard equivalent [ed: ascii code] in javascript

I'm trying to check if a variable exists in the LMS (BrainHoney) that our school is using. The variab开发者_如何转开发les are surrounded by dollar signs. In order to see if the variable is in use I would like to try an if statement to the effect of alert("$Hello$" == "& #36;Hello& #36;");

If the variable wasn't in use, $Hello$ would remain $Hello$ instead of being replaced by a different string of text.

Unfortunately this returns false because they are not the same. What should I change so that if the LMS leaves the text string as $Hello$ that I could check for it?


alert("\044Hello\044" == "$Hello$");

Use the ascii value of $ in octal escaped by javascript.

Or:

alert("$Hello$" == "$Hello$");

This is the ascii value of $ in hex, escaped by the html.


$ is not an html special character and the html will probably maintain the $ and not have the encoded representation so you probably need to use eval function for this (unless you want to pollute your global array with all these strings which is a bad idea).

assuming $Hello$ is instantiated somewhere like $Hello$ = 'Hi' and you are parsing through html and you come across a potential replacement and put it into a var called lookupStr, you could do the following.

if ( typeof( eval(lookupStr) ) != "undefined")
    replacement = lookupStr;
else
    eval(replacement = eval(lookupStr));

Then just replace the html portion with the replacement variable.


If you're reasonably sure the replacement won't start with a dollar sign, how about

"$Hello$".charAt(0) == "$"

?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜