Firefox是否支持XHTML页面中的Javascript?
我需要提供包含Javascript的XHTML页面。我的问题是Firefox(3.5.7)似乎忽略了Javascript。例如:Firefox是否支持XHTML页面中的Javascript?,javascript,xml,firefox,xhtml,Javascript,Xml,Firefox,Xhtml,我需要提供包含Javascript的XHTML页面。我的问题是Firefox(3.5.7)似乎忽略了Javascript。例如: <?xml version="1.0"?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>My Title</title> </head> <body> <script type="
<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Title</title>
</head>
<body>
<script type="text/javascript">
document.write("Hello world!");
</script>
</body>
</html>
我的头衔
写下“你好,世界!”;
如果我将其保存为test.html
,Firefox会正确地显示它。如果我将其保存为test.xml
,Firefox将显示一个空白页面。我做错了什么?来自
document.write在XHTML中工作吗?
不可以。由于XML的定义方式,不可能实现这样的技巧,即在解析器仍在解析标记时通过脚本生成标记
您仍然可以实现相同的效果,但必须使用DOM添加和删除元素
document.write
不起作用,但设置innerHTML
仍在起作用。只要记住,无论你在那里设置什么,都必须有良好的格式
这意味着您可以使用jQuery向任何元素添加新元素/设置html
我有一个小图书馆用来做这个。这很管用
function IsXHTML() {
if (document.doctype == null)
return false;
var vPublic = document.doctype.publicId.replace(/^.*(.html).*$/i, '$1').toLowerCase();
var vSystem = document.doctype.systemId.replace(/^.*(.html).*$/i, '$1').toLowerCase();
return (vPublic == 'xhtml')
&& (vSystem == 'xhtml');
};
function XHTMLDocWrite($Str, $Element) {
if ($Str == null)
return;
var vIsMozilla = !window.opera && !/Apple/.test(navigator.vendor);
var vIsOpera = window.opera;
// Make sure &s are formatted properly
if (!vIsOpera)
$Str = $Str.replace(/&(?![#a-z0-9]+;)/g, "&");
else $Str = $Str.replace(/&(?![#a-z0-9]+;)/g, "&");
// - Mozilla assumes that everything in XHTML, innerHTML is actually XHTML
// - Opera and Safari assume that it's XML
if ( !vIsMozilla )
$Str = $Str.replace(/(<[a-z]+)/g, "$1 xmlns='http://www.w3.org/1999/xhtml'");
// The HTML needs to be within a XHTML element
var vDiv = document.createElementNS("http://www.w3.org/1999/xhtml","div");
vDiv.innerHTML = $Str;
if ($Element == null) {
// Find the last element in the document
var vLastElmt = document.getElementsByTagName("*");
vLastElmt = vLastElmt[vLastElmt.length - 1];
$Element = vLastElmt;
}
// Add all the nodes in that position
var vNodes = vDiv.childNodes;
while (vNodes.length)
$Element.parentNode.appendChild( vNodes[0] );
};
function UseXHTMLDocWrite($IsForced) {
if (!IsXHTML() && !$IsForced)
return;
if (document.write == XHTMLDocWrite)
return;
document.write = XHTMLDocWrite;
};
函数IsXHTML(){
if(document.doctype==null)
返回false;
var vPublic=document.doctype.publicId.replace(/^.*.html.*$/i,$1').toLowerCase();
var vSystem=document.doctype.systemId.replace(/^..*.html.*$/i,$1').toLowerCase();
返回(vPublic=='xhtml')
&&(vSystem=='xhtml');
};
函数XHTMLDocWrite($Str,$Element){
如果($Str==null)
返回;
var vIsMozilla=!window.opera&!/Apple/.test(navigator.vendor);
var vIsOpera=window.opera;
//确保&s的格式正确
如果(!vIsOpera)
$Str=$Str.replace(/&(?![#a-z0-9]+)/g,“&;”;
否则$Str=$Str.replace(/&(?![#a-z0-9]+)/g,“&”);
//-Mozilla假设XHTML、innerHTML中的所有内容实际上都是XHTML
//-Opera和Safari假设它是XML
如果(!vIsMozilla)
$Str=$Str.replace(/(天哪,这是否意味着有那么多(大多数?)Javascript库根本不起作用?我希望大多数Javascript库不会使用document.write。这很糟糕。答案的第二个重要部分:这并不意味着你不能将Javascript与XHTML结合使用,它只是意味着你不能使用document.write
。相反,正如W3C引用所说,一旦页面加载,你就可以使用它DOM操作以添加/修改内容。在XHTML文件本身中编写JavaScript有点麻烦;最好将其定位在单独的JavaScript文件中,并使用script
标记的src
属性将其引入。只是不要使用document.write()。太棒了。但如果您将脚本按原样包含在页面中,请记住将其包装在CDATA中,特别是用于XHTML的脚本。