Javascript 和 HTML:将文件保存为无 BOM 的 UTF-8

发布于 2024-09-28 13:24:09 字数 2176 浏览 1 评论 0原文

我正在尝试编写一个仅限 MSIE 的 HTML 页面(我将其称为“标题页”),该页面允许某人通过单击按钮来保存生成的 HTML 网页(我将其称为“新页面”)。

我发现出现的“另存为”对话框不允许将“新页面”保存为没有 BOM 的 UTF-8。相反,它被保存为 Unicode(UTF-8),而浏览器又将其确定为 UTF-16。下面是“标题页”的示例代码:

<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>

我用 http://web-sniffer.net 检查了“新文件” / 它告诉我“新文件”是以这种方式保存的:

<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>

我能做些什么来阻止它在我这边将“新页面”保存为 UTF-16 吗?还是和代码无关?

另外,“另存为”对话框中有一个下拉菜单,据说可以让我更改字符编码,但实际上所做的只是保存“标题页”而不是“新页面”。

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."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑,眼淚并存 2024-10-05 13:24:10

尝试将页面保存在其他编辑器中,例如 Notepad++,其中设置特定编码会更清晰:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文