Javascript Encoding Question

We have an external .js file that we want to include in several different pages. The file contains code to sort the 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 the ASP.Net page to offload some sorting work from server to client (disable postbacks sorting with javascript enabled). In this case, the encoding is almost always UTF-8, and in this context, it works great.

However, we also have some old ASP pages where we want to include a script. For these pages, coding is rather a mishap depending on who wrote the page, when and which tool they used (notepad, vs6, vs2005, another html helper). Often, no encoding is indicated on the page, so you can choose in the browser, but there is no hard rule for this that I can see.

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

How can I configure the script for various possible encodings so that the "special" characters always display correctly? Are there any different characters that I could use, or a trick I missed to make html objects work with javascript?

Here is a snippet that uses characters:

// 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 codes that I used with the accepted answer were \ u25B4 and \ u25BC.

+3
source share
3 answers

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

+6
source

JavaScript HTML-, . JavaScript UTF-8 HTML- ISO-8859-1, JavaScript ISO-8859-1.

JavaScript , JavaScript:

<script type="text/javascript" charset="UTF-8" src="externalJS.js"></script>

, - , UTF-8.

+7

. , .

, , script, UTF-8 UTF-8. , , .

- . , , , .

+1
source

Source: https://habr.com/ru/post/1703812/


All Articles