当前位置: 动力学知识库 > 问答 > 编程问答 >

Javascript and HTML: Saving file as UTF-8 without BOM

问题描述:

I'm trying to write an MSIE only HTML page (which I'll call the "Title Page") that allows someone to save a generated HTML webpage (which I'll call "New Page") with a click of a button.

What I found out is that the "Save As" dialog box that appears does not allow for the "New Page" to be saved as UTF-8 without BOM. It is instead, being saved as Unicode(UTF-8) which in turn has browsers determining it to be UTF-16. Below is a sample code of the "Title Page":

<html>

<head>

<script>

/**

*Takes the information in the form and arranges it in the proper format.

*/

function save()

{

str = "Hello World";

mydoc = document.open();

mydoc.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");

mydoc.write("\r\n\r\n");

mydoc.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n\t<head>\r\n\r\n\r\n\r\n\r\n<!--LOOK FOR \"EDIT HERE\"//-->\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\r\n\r\n<title>");

/*Inserts str in the title*/

mydoc.write(str);

mydoc.write("</title>\r\n\r\n\t</head>\r\n\r\n\r\n\t<body>\r\n\r\n\r\n<!--EDIT HERE//-->\r\n\r\n<H1>");

/*Inserts form variables from here*/

mydoc.write(str);

mydoc.write("</H1>\r\n\r\n\r\n<!--DO NOT EDIT BEYOND THIS POINT//-->\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n</body>\r\n</html>\r\n\r\n");

mydoc.execCommand("saveAs",true,"*.html");

mydoc.close();

}

</script>

</head>

<body>

<p align=right><input type="button" value="save" onclick="save()"></p>

</body>

</html>

I checked "New File" with http://web-sniffer.net/ and it tells me that "New File" is being saved this way:

<code>ÿþ!<�D�O�C�T�Y�P�E� �h�t�m�l� �P�U�B�L�I�C� �"�-�/�/�W�3�C�/�/�D�T�D� �X�H�T�M�L� �1�.�0 �T�r�a�n�s�i�t�i�o�n�a�l�/�/�E�N�"� �"�h�t�t�p�:�/�/�w�w�w�.�w�3�.�o�r�g�/�T…</code>

Is there anything I can do to stop it from saving "New Page" as UTF-16 on my end? Or does it have nothing to do with the code?

Also, there's a drop down in the "Save As" dialog box that supposedly allows me to change the character encoding, but all that really does is save the "Title Page" not the "New Page."

网友答案:

Try saving the page in a different editor, such as Notepad++, where setting the specific encoding is much clearer:

http://download.cnet.com/Notepad/3000-2352_4-10327521.html

分享给朋友:
您可能感兴趣的文章:
随机阅读: