Jump to content

how to find a tag


nasi

Recommended Posts

I have used the following code to access the content of a webpage.<html> <head> <script type="text/javascript"> function displayUrl() { if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari var req = new XMLHttpRequest(); } else {// code for IE6, IE5 var req = new ActiveXObject("Microsoft.XMLHTTP"); } req.open('GET', 'http://webpage.com', true); req.onreadystatechange = function (aEvt) { if (req.readyState == 4) { if(req.status == 200) txt=req.responseText; else dump("Error loading page\n"); } req.send(null); } </script> </head> <body> <p id="demo"></p> <button type="button" onclick="displayUrl()">Display Url</button> </body> </html> my problem now is to find all <a href...</a> in "txt" and add a check box before them. can you please help me?

Link to comment
Share on other sites

If you are trying to get all the anchors from ajax response as text there is one way parsing the response text using regular expression. If you want to use DOM which is better to take out the elements you need to take the response as XML. You can only take a response as XML if the requested document content type is XML based.If the document xontent type is other than XML based you need to change the content type. If you take the request as XML yo can then use getElemetnsByTagName() to get the list of anchors and can implement the DOM functions.

Link to comment
Share on other sites

If you are trying to get all the anchors from ajax response as text there is one way parsing the response text using regular expression. If you want to use DOM which is better to take out the elements you need to take the response as XML. You can only take a response as XML if the requested document content type is XML based.If the document xontent type is other than XML based you need to change the content type. If you take the request as XML yo can then use getElemetnsByTagName() to get the list of anchors and can implement the DOM functions.
Thanks. I have changed my code to the following but getElemetnsByTagName() doesn't print any thing.<html><head><script type="text/javascript">function displayUrl(){ if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari var req = new XMLHttpRequest(); } else {// code for IE6, IE5 var req = new ActiveXObject("Microsoft.XMLHTTP"); } req.open('GET', 'www.webpage.com', true); req.onreadystatechange = function (aEvt) { if (req.readyState == 4) { if(req.status == 200) txt=req.responseText; else dump("Error loading page\n"); } if (window.DOMParser) { parser=new DOMParser(); xmlDoc=parser.parseFromString(txt,"text/xml"); } else // Internet Explorer { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async="false"; xmlDoc.loadXML(txt); } document.write(xmlDoc.getElementsByTagName("<a")); } req.send(null);}</script></head><body><p id="demo"></p><button type="button" onclick="displayUrl()">Display Url</button></body></html>
Link to comment
Share on other sites

document.write(xmlDoc.getElementsByTagName("<a"));
it should be something like this..
document.write(xmlDoc.getElementsByTagName("a"));

getElementsByTagName() will return an array of element list. If you wan to access each element of it you need to specify its index.

Link to comment
Share on other sites

it should be something like this..
document.write(xmlDoc.getElementsByTagName("a"));

getElementsByTagName() will return an array of element list. If you wan to access each element of it you need to specify its index.

Thanks at least now it prints something. but it just prints "object".
Link to comment
Share on other sites

getElementsByTagName() will return an array of element list. If you wan to access each element of it you need to specify its index.
You can loop through all of them using a for loop.
Link to comment
Share on other sites

Thanks at least now it prints something. but it just prints "object".
right. as he said, it returns an array. Now you need to search through it to find what you are looking for.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...