使用 javascript 设置文档类型 [英] set doctype using javascript

查看:49
本文介绍了使用 javascript 设置文档类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个没有 doctype 声明部署到服务器的 html 页面(比如 A).这是从另一台服务器(比如 B)获取 js 文件.Node.js 创建必要的 html 页面来显示.现在 IE8 正在创建问题,因为没有声明 doctype(将自身设置为 IE5 怪癖模式)

I have a html page with no doctype declared deployed to a server(say A). This is fetching js files from another server(say B). js creates necessary html page to display. Now IE8 is creating problems as there is not doctype declared(sets itself to IE5 quirks mode)

现在 doctype 是读取的第一行,这似乎不可能通过这种方式完成(使用 js 设置 doctype).是否可以设置元标记来将页面设置为标准模式?或者有没有其他我可以将页面设置为标准页面而无需修改来自服务器 A 的 html 页面.

Now doctype is the first line read, and this seems impossible to be done this way(using js to set the doctype). Is it possible to set a meta tag instead to set the page to standards mode? Or is there any other i can set the page to standard page without modifying the html page from server A.

推荐答案

var nodeDoctype = document.implementation.createDocumentType(
 'html',
 '-//W3C//DTD XHTML 1.0 Transitional//EN',
 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtdd'
);
if(document.doctype) {
    document.replaceChild(nodeDoctype, document.doctype);
} else {
    document.insertBefore(nodeDoctype, document.childNodes[0]);
}

根据您的评论更新:

可以使用 JS 更改 doctype 以启用兼容性查看(如下所示:http://www.webmasterworld.com/forum91/4856.htm)但这是一个非常讨厌的黑客,不推荐.理想情况下,您可以执行此服务器端.所以有一个 doctype js 参数,然后重新加载页面:

It is possible to change the doctype with JS to enable compatability viewing (as done here: http://www.webmasterworld.com/forum91/4856.htm) but is quite a nasty hack and not recommended. Ideally you can do this server side. So have a doctype js parameter and then do a page reload:

window.location = window.location+"?doctype=newdoctype"

这将导致页面重新加载,这可能不适合您,但这是最安全的方法.

This will cause the page to reload which might not suit you, but is the safest way to do it.

这篇关于使用 javascript 设置文档类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆