As the title describes, here is my basic xhtml page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>An XHTML 1.0 Strict standard template</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script type="text/javascript">
var headings = document.evaluate('//h2', document, null, XPathResult.ANY_TYPE, null );
var thisHeading = headings.iterateNext();
var alertText = 'Level 2 headings in this document are:\n'
while (thisHeading) {
alertText += thisHeading.textContent + '\n';
thisHeading = headings.iterateNext();
}
console.log(alertText);
</script>
</head>
<body>
<h2>… Your HTML content here …</h2>
</body>
</html>
Output is:
Level 2 headings in this document are:
web browsers process the html from top to bottom. Your script runs before the rest of the page exists. Move it down, or execute the code on the window onload event.