getElementsByTagName出错

I am trying to read in a html file, and then only display certain parts on the page.

I have currently read in the file using AJAX and the requestData.js file. I can then alert out the whole page using that html variable I have created and that works fine.

However it is then when I try and get all the <p> Tags (or any other tag doesn't work), that it comes up with this error.

TypeError: 'undefined' is not a function (evaluating 'html.getElementsByTagName("div")')

What am I doing wrong here?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<script type="text/javascript" language="JavaScript" src="requestData.js"></script>
<script type = "text/javascript">

    function refresh(xmlhttp){
        var html = xmlhttp.responseText;
        var ps = html.getElementsByTagName("p");
    }

</script>

</head>
<body>

</body>

<input type = "submit" onclick = "requestData('home.html', refresh)">

</html>

html var its just string, use jQuery Framework for parsing it :) Like this jQuery(html).Find('p');

You can create an element and set its content to your HTML. This will give you the ability to run getElementsByTagName() on it:

var html = xmlhttp.responseText;

var div = document.createElement('div');
div.innerHTML = html;

var ps = div.getElementsByTagName("p");

responseText is a string. You need to "parse" it to be able to call DOM methods on it.

One way is to make a "fake" <div> then append the string to it.

var html = xmlhttp.responseText;
var parse = document.createElement('div');
parse.innerHTML = html;

var ps = parse.getElementsByTagName("p");