⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dom_errors_crossbrowser.asp@output=print

📁 W3Schools tutorial..web designing
💻 ASP@OUTPUT=PRINT
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>XML DOM Parser Errors</title>
 
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />

<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />

<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>

</head>
<body>

<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>


<h1>XML DOM Parser Errors</h1>

<a href="dom_errors.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_summary.asp"><img border="0" src="../images/btn_next.gif" alt="next" width="100" height="20" /></a>

<hr />

<p class="intro">When Firefox encounter a parser error, it loads an XML document 
containing the error</p>
<hr />

<h2>Parser Error in Firefox</h2>

<p>When trying to open an XML document, a parser-error may occur.</p>
<p>Unlike Internet Explorer, if Firefox encounters an error, it loads an XML 
document containing the error description.</p>
<p>The root node name of the XML error document is &quot;parsererror&quot;. This is used 
to check if there is an error.</p>

<hr />
<h2>XML Error
</h2>

<p>In the following code we let the parser load an XML document that is not well-formed.
</p>

<p>(You can read more about well-formed and valid XML in our
<a href="../xml/default.asp">XML tutorial</a>)
</p>

<table border="1" width="100%" class="ex" cellspacing="0"><tr><td>
<pre>xmlDoc=document.implementation.createDocument(&quot;&quot;,&quot;&quot;,null);
xmlDoc.async=false;
xmlDoc.load(&quot;note_error.xml&quot;);

if (xmlDoc.documentElement.nodeName==&quot;parsererror&quot;)
{
errStr=xmlDoc.documentElement.childNodes[0].nodeValue;
errStr=errStr.replace(/&lt;/g, &quot;&amp;lt;&quot;);
document.write(errStr);
}
else
{
document.write(&quot;XML is valid&quot;);
}</pre></td></tr></table>

<p>Example explained:</p>
<ol>
	<li>Load the xml file</li>
	<li>Check if the nodeName of the root node is &quot;parsererror&quot;</li>
	<li>Load the error string into a variable &quot;errStr&quot;</li>
	<li>Replace &quot;&lt;&quot; characters with &quot;&amp;lt;&quot; before the error string can be 
	written as HTML</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=note_error_firefox">Try it yourself</a>
or just <a target="_blank" href="note_error.xml">look at the XML file</a></p>
<p><b>Note:</b> Only Internet Explorer will actually check your XML against the 
DTD. Firefox will not.</p>

<hr />
<h2>Cross Browser Error Check</h2>
<p>Here we have created an XML load function that checks for parser errors in 
both Internet Explorer and Firefox:</p>

<table width="100%" border="1" class="ex" cellspacing="0" id="table1">
<tr><td>
<pre>function loadXMLDocErr(dname) 
{
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
  xmlDoc.async=false;
  xmlDoc.load(dname); 

  if (xmlDoc.parseError.errorCode != 0) 
    {
    alert(&quot;Error in line &quot; + xmlDoc.parseError.line + 
    &quot; position &quot; + xmlDoc.parseError.linePos + 
    &quot;\nError Code: &quot; + xmlDoc.parseError.errorCode + 
    &quot;\nError Reason: &quot; + xmlDoc.parseError.reason + 
    &quot;Error Line: &quot; + xmlDoc.parseError.srcText);
    return(null);
    }
  }
catch(e)
  {
  try //Firefox
    {
    xmlDoc=document.implementation.createDocument(&quot;&quot;,&quot;&quot;,null);
    xmlDoc.async=false;
    xmlDoc.load(dname);
    if (xmlDoc.documentElement.nodeName==&quot;parsererror&quot;)
      {
      alert(xmlDoc.documentElement.childNodes[0].nodeValue);
      return(null);
      }
    }
  catch(e) {alert(e.message)}
  }
try 
  {
  return(xmlDoc);
  }
catch(e) {alert(e.message)}
return(null);
}</pre>
</td></tr>
</table>
<p>Example explained - Internet Explorer:</p>
<ol>
	<li>The first line creates an empty Microsoft XML document object</li>
	<li>The second line turns off asynchronized loading, to make sure that the 
	parser will not continue execution of the script before the document is 
	fully loaded</li>
	<li>The third line tells the parser to load an XML document called &quot;note_error.xml&quot;</li>
	<li>If the errorCode property of the parseError object is different from 
	&quot;0&quot;, alert the error and exit the function</li>
	<li>If the errorCode property is &quot;0&quot;, return the XML document</li>
</ol>
<p>Example explained - Firefox:</p>
<ol>
	<li>The first line creates an empty XML document object.</li>
	<li>The second line turns off asynchronized loading, to make sure that the 
	parser will not continue execution of the script before the document is 
	fully loaded.</li>
	<li>The third line tells the parser to load an XML document called &quot;note_error.xml&quot;.</li>
	<li>If the returned document is an error document, alert the error and exit 
	the function</li>
	<li>If not, return the XML document</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=note_error_crossbrowser">Try it yourself</a>
or just <a target="_blank" href="note_error.xml">look at the XML file</a><br />
</p>
<hr />
<a href="dom_errors.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_summary.asp"><img border="0" src="../images/btn_next.gif" alt="next" width="100" height="20" /></a>

<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -