javascript的html编码函数

 function htmlspecialchars(str)  

    {  

        var s = "";

        if (str.length == 0) return "";

        for   (var i=0; i<str.length; i++)

        {

            switch (str.substr(i,1))

            {

                case "<": s += "&lt;"; break;

                case ">": s += "&gt;"; break;

                case "&": s += "&amp;"; break;

                case " ":

                    if(str.substr(i + 1, 1) == " "){

                        s += " &nbsp;";

                        i++;

                    } else s += " ";

                    break;

                case "\"": s += "&quot;"; break;

                case "\n": s += "<br>"; break;

                default: s += str.substr(i,1); break;

            }

        }

        return s;

    }

posted @ 2009-02-25 11:13  Rick Sun  阅读(187)  评论(0编辑  收藏  举报