-3

Here is what I am trying but am not sure how to get this working or if it is even possible -

  1. I have an HTML page MyHTMLPage.htm and I want to src a Javascript from this HTML file. This is pretty straightforward. I plan to include a <script src = "MyJavascript.js"></script> tag in my HTML file and that should take care of it.

  2. However, I want to create my Javascript file using UTF-16 encoding. So, I plan to use the following tag <script charset="UTF-16" src="MyJavascript.js"></script> in my HTML file to take care of that

  3. Now the problem I am really stuck at is how do I create the Javascript using UTF-16 encoding - E.g. let's say my Javascript code is alert(1); I created my Javascript file with the contents as \u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0029\u003b but that does not seem to execute as valid Javascript at runtime.

To summarize, here is what I have -

MyHTMLPage.html

...
...
...
<script charset="UTF-16" src="MyJavascript.js"></script>
...
...
...

MyJavascript.js

\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0029\u003b

When I open the HTML page in Firefox, I get the error - "Syntax error - Illegal character" right at the beginning of the MyJavascript.js file. I have also tried adding the BOM character "\ufeff" at the beginning of the above Javascript but I still get the same error.

I know I could create my Javascript file as - "alert(1);" and then save it using UTF-16 encoding using the text editor and then the browser runs it fine however is there a way I could use "\u" notation (or an alternate escape character) and still get the Javascript to execute fine?

Thanks,

user967973
  • 2,475
  • 2
  • 18
  • 13
  • You're not trying to secure your JavaScript code, are you? – I Hate Lazy Oct 22 '12 at 14:51
  • are you just trying to obfuscate your JS?? if so, try [this](http://www.jsobfuscate.com/index.php) or [this](http://www.javascriptobfuscator.com/default.aspx) or if all else fails, there's always [this](https://www.google.com/search?q=obfuscate+javascript) – davehale23 Oct 22 '12 at 14:57

1 Answers1

2

You are misunderstanding character encoding. Character encoding is a scheme of how characters are represented as bits behind the scenes.

You would not write \u004a in your file to "make it utf-16" as that is literally a sequence of 6 characters:

\, u, 0, 0, 4, a

And if you saved the above as utf-16, it would be represented as the following bits:

005C0075
00300030
00340061

Had you saved it as utf-8 it would be:

5C753030
3461

Which takes 50% of the space and bandwidth. It takes even less to write that character literally ("J"): just a byte (4A) in utf-8.

The "\u"-notation is a way to reference any BMP character by just using a small set of ascii characters. If you were working with a text editor with no unicode support, you could write "\u2665", instead of literally writing "♥" and the browser would show it properly.

If you for some weird reason still want to use utf-16, simply write the code normally, save the file as utf-16 and serve it with the proper charset header.

Esailija
  • 138,174
  • 23
  • 272
  • 326