views:

916

answers:

3

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.

+5  A: 

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.

andynormancx
Thanks, that did it. For the curious, the codes in question where \u25B4 and \u25BC
Joel Coehoorn
+5  A: 

The encoding of the JavaScript file depends on the encoding of the HTML page, where it is embedded. If you have a UTF-8 JavaScript file and a ISO-8859-1 HTML page the JavaScript is interpreted as ISO-8859-1.

If you load the JavaScript from as a external file you could specify the encoding of the JavaScript:

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

Anyway the best option is to save all files related to a webproject in one encoding, UTF-8 recommended.

Hippo
A: 

I voted for both. I think both answers put together would be your best bet.

You're probably going to have to write the script twice, putting in a part for UTF-8, and putting in a part for non UTF-8. It's more trouble, and might not work all the time, STILL.

Someone needs to come up with standards for your developers. If you all write with at least the same encoding, it'll make things a lot easier for yourselves in the future.

EllaJo
You don't need two different scripts. The Javascript Unicode escape approach works whether or not the page the Javascript is loaded into is UTF-8 or not. I wasn't 100% sure this was the case, so I tested in FF3 and IE7 before posting my answer.
andynormancx
But I would agree that fixing the pages so they are all UTF-8 is a better long term solution.
andynormancx