这个JavaScript代码是什么意思? “使用String.fromCharCode”

时间:2016-06-19 17:38:35

标签: javascript string fromcharcode

我在HTML文件中发现了这段代码,我不太明白这意味着什么。有没有人有想法?谢谢!

<script language="JavaScript" type="text/javascript"> 
t="102,117,110,99,116,105,111,110,32,115,101,108,101,99,116,115,116,97,114,40,99,95,110,97,109,101,44,118,97,108,117,101,44,101,120,112,105,114,101,100,97,121,115,41,10,123,10,118,97,114,32,101,120,100,97,116,101,61,110,101,119,32,68,97,116,101,40,41,10,101,120,100,97,116,101,46,115,101,116,68,97,116,101,40,101,120,100,97,116,101,46,103,101,116,68,97,116,101,40,41,43,101,120,112,105,114,101,100,97,121,115,41,10,100,111,99,117,109,101,110,116,46,99,111,111,107,105,101,61,99,95,110,97,109,101,43,32,34,61,34,32,43,118,97,108,117,101,43,32,40,40,101,120,112,105,114,101,100,97,121,115,61,61,110,117,108,108,41,32,63,32,34,34,32,58,32,34,59,101,120,112,105,114,101,115,61,34,43,101,120,100,97,116,101,46,116,111,71,77,84,83,116,114,105,110,103,40,41,41,43,34,59,112,97,116,104,61,47,34,10,125,10";
t=eval("String.fromCharCode("+t+")");
document.write("<script>"+t+"<\/script>");
</script>

6 个答案:

答案 0 :(得分:1)

此方法converts a unicode number into a character,因此带有数值的字符串将更改为unicode字符序列。

答案 1 :(得分:1)

每个字符(A,B,C及其小写字母a,b,c)由一个数字定义(参见ASCII表)。

例如,A(大写)是数字65.就像在编程语言中不支持的字符而我们不得不使用数字序列的时间。

String.fromCharCode将把字母的代码作为参数并将其转换为字符串。 (注意它返回一个字符串而不是String对象。)

在提出这些问题之前,请务必检查Mozilla开发者网络,答案很可能是:)

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode

您的代码片段将转换为函数:

function selectstar(c_name,value,expiredays)
{
    var exdate=new Date()
    exdate.setDate(exdate.getDate()+expiredays)
    document.cookie=c_name+ "=" +value+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/"
}

答案 2 :(得分:1)

String.fromCharCode()只是将Unicode编号转换为字符。 有关更多说明,请参阅this

答案 3 :(得分:1)

String.fromCharCode 会将给定的整数值转换为关联的字符。

关于这个例子,我不确定你到底在寻找什么。你能否详细说明你想要实现的目标,如描述场景

答案 4 :(得分:1)

String.fromCharCode()方法返回使用指定的Unicode值序列创建的字符串。

var result = String.fromCharCode(102,117,110,99,116,105,111,110,32,115,101,108,101,99,116,115,116,97,114,40,99,95,110,97,109,101,44,118,97,108,117,101,44,101,120,112,105,114,101,100,97,121,115,41,10,123,10,118,97,114,32,101,120,100,97,116,101,61,110,101,119,32,68,97,116,101,40,41,10,101,120,100,97,116,101,46,115,101,116,68,97,116,101,40,101,120,100,97,116,101,46,103,101,116,68,97,116,101,40,41,43,101,120,112,105,114,101,100,97,121,115,41,10,100,111,99,117,109,101,110,116,46,99,111,111,107,105,101,61,99,95,110,97,109,101,43,32,34,61,34,32,43,118,97,108,117,101,43,32,40,40,101,120,112,105,114,101,100,97,121,115,61,61,110,117,108,108,41,32,63,32,34,34,32,58,32,34,59,101,120,112,105,114,101,115,61,34,43,101,120,100,97,116,101,46,116,111,71,77,84,83,116,114,105,110,103,40,41,41,43,34,59,112,97,116,104,61,47,34,10,125,10);
console.log(result);
 

答案 5 :(得分:0)

这里的变量t是一个代表unicode / ascii字符的数字列表,即116 ='N'。

它转换为:

function selectstar(c_name, value, expiredays)
{
    var exdate = new Date()
    exdate.setDate(exdate.getDate() + expiredays)
    document.cookie = c_name + "=" + value + ((expiredays==null) 
      ? "" : ";expires=" + exdate.toGMTString()) + ";path=/"
}

它尝试评估代码段并将其插入页面。