本文介绍了Javascript编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个外部的.js文件,我们要包含在多个不同的页面中。该文件包含用于在客户端对表进行排序的代码,并使用脚本中的▲和▼字符来指示哪一列被排序以及在哪个方向。



该脚本最初是为ASP.Net页面编写的,用于将一些排序工作从服务器卸载到客户端(当启用JavaScript时,防止排序回发)。在这种情况下,编码几乎总是UTF-8,它在这种情况下很好。



但是,我们还有一些较旧的Classic ASP页面,我们希望包含该脚本。对于这些页面,编码更多是一个大杂烩,具体取决于谁在写页面的时间和使用的工具(记事本,vs6,vs2005,其他html帮助器)。通常在页面中没有指定编码,所以浏览器是可以选择的,但实际上没有什么硬规则可以看到。



问题是如果使用不同的(非UTF8)编码,▼和▲字符将不会正确显示。我尝试使用html实体,但是不能让它们从javascript起作用。



如何使脚本针对各种潜在的编码进行调整,以便特殊字符总是正常显示?是否有不同的字符我可以使用,或者我错过了使HTML实体工作从javascript的一个技巧?



这里是使用字符的片段: p>

  //获取排序方向,箭头
var dir = 1;
if(self.innerHTML.indexOf(▲)> -1)
dir = -1;
var arrow =(dir == 1)?▲:▼;

// SORT - 实际排序的函数 - 与问题
无关if(!SimpleTableSort(t.id,self.cellIndex,dir,sortType))返回;

//删除所有箭头
(var c = 0,cl = t.rows [0] .cells.length; c {
var cell = t.rows [0] .cells [c];
cell.innerHTML = cell.innerHTML.replace(▲,).replace(▼,);
}

//设置新的箭头
self.innerHTML + = arrow;对于好奇,我最终用接受的答案使用的代码点是\uBB和\ \\ \\ u25BC。

解决方案

您想要JavaScript Unicode转义即\uxxxx,其中xxxx是Unicode代码点人物。我相信\25B2和\25BC是你需要的两个。


We have an external .js file that we want to include in a number of different pages. The file contains code for sorting a table on the client-side, and uses the ▲ and ▼ characters in the script to indicate which column is sorted and in which direction.

The script was originally written for an ASP.Net page to offload some sorting work from the server to client (prevent sorting postbacks when javascript is enabled). In that case, the encoding is pretty much always UTF-8 and it works great in that context.

However, we also have a number of older Classic ASP pages where we want to include the script. For these pages the encoding is more of a hodgepodge depending on who wrote the page when and what tool they were using (notepad, vs6, vs2005, other html helper). Often no encoding is specified in the page so it's up to the browser to pick, but there's really no hard rule for it that I can see.

The problem is that if a different (non-UTF8) encoding is used the ▼ and ▲ characters won't show up correctly. I tried using html entities instead, but couldn't get them to work well from the javascript.

How can I make the script adjust for the various potential encodings so that the "special" characters always show up correctly? Are there different characters I could be using, or a trick I missed to make the html entities work from javascript?

Here is the snippet where the characters are used:

// get sort direction, arrow
var dir = 1;
if (self.innerHTML.indexOf(" ▲") > -1)
     dir = -1;
var arrow = (dir == 1)?" ▲":" ▼";

// SORT   -- function that actually sorts- not relevant to the question
if  (!SimpleTableSort(t.id, self.cellIndex, dir, sortType)) return;

//remove all arrows
for (var c = 0,cl=t.rows[0].cells.length;c<cl;c+=1)
{
    var cell = t.rows[0].cells[c];
    cell.innerHTML = cell.innerHTML.replace(" ▲", "").replace(" ▼", "");
}

// set new arrow
self.innerHTML += arrow;

For the curious, the code points I ended up using with the accepted answer were \u25B4 and \u25BC.

解决方案

You want Javascript Unicode escapes i.e. "\uxxxx", where "xxxx" is the Unicode code point for the character. I believe "\u25B2" and "\u25BC" are the two you need.

这篇关于Javascript编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 19:42