如何在网页上显示格式正确的JS代码片段?

时间:2021-03-03 15:49:26

I am storing a JavaScript function in DB without spaces. Later I am retrieving from DB and showing on a Web page using google prettify but the code-snippet shows in a single line,

我在没有空格的DB中存储JavaScript函数。后来我从数据库中检索并使用谷歌美化显示在网页上,但代码片段显示在一行中,

var Pizza = function(size, toppingCodes) {
  this.size = size;
  this.toppingCodes = toppingCodes;
  this.toString = function() {
    return "A " + size + " pizza with " + this.toppingCodes + ".";
  };
};
Pizza.prototype.getBaseCost = function() {
  switch (this.size) {
    case "small":
      return 6.50;
    case "medium":
      return 7.50;
    case "large":
      return 8.50;
  }
};

I am getting the key-word color etc by using google prettify. Can I show the above JS function in a nicely formatted way. DO we have a tool/library for this ??

我正在使用谷歌美化获得关键词颜色等。我能以一种格式很好的方式显示上面的JS函数吗?我们有一个工具/库吗?

1 个解决方案

#1


0  

I finally figured it out. when saving into database, I need to encode it using encodeURI(string) and when fetch from the server back, I need to decode it using decodeURI(string).

我终于弄明白了。保存到数据库时,我需要使用encodeURI(字符串)对其进行编码,当从服务器获取时,我需要使用decodeURI(字符串)对其进行解码。

#1


0  

I finally figured it out. when saving into database, I need to encode it using encodeURI(string) and when fetch from the server back, I need to decode it using decodeURI(string).

我终于弄明白了。保存到数据库时,我需要使用encodeURI(字符串)对其进行编码,当从服务器获取时,我需要使用decodeURI(字符串)对其进行解码。