943,733 Members | Top Members by Rank

Ad:
May 24th, 2009
0

Parsing an HTML String

Expand Post »
Hi,

I need to parse an HTML string received from an AJAX request. I wrote a function that places the HTML string into an unappended (not added) <div> , which opens the string up to the DOM hierarchy. However, when I try to access the elements of this <div> , I get an error in the console that says root.getElementById is not a function. This tells me that I can't access any of the child nodes.

Can anyone help? Here is what my script looks like:
JavaScript Syntax (Toggle Plain Text)
  1. function parseHTML(html) {
  2. var root = document.createElement("div");
  3. root.innerHTML = html;
  4.  
  5. // The error console stops at this line
  6. if (root.getElementById("head")) {
  7. // Parsing head...
  8. // Functions removed (unnecessary to post)
  9.  
  10. // Parsing finished, remove tag
  11. root.removeChild(head);
  12. }
  13.  
  14. document.body.innerHTML = root.innerHTML;
  15. }
Does anybody know why I can't access DOM functions with root ?
Last edited by itsjareds; May 24th, 2009 at 1:53 am.
Similar Threads
Reputation Points: 39
Solved Threads: 13
Junior Poster
itsjareds is offline Offline
103 posts
since May 2009
May 24th, 2009
1

Re: Parsing an HTML String

getElementById() only works with document. To parse your HTML can use childNodes;

Modifying your code all little bit:

javascript Syntax (Toggle Plain Text)
  1. function parseHTML(html) {
  2. var root = document.createElement("div");
  3. root.innerHTML = html;
  4.  
  5. // Get all child nodes of root div
  6. var allChilds = root.childNodes;
  7. // Loop through all the child nodes and check for id = 'head'
  8. for(var i = 0; i < allChilds.length; i++) {
  9. // if id == 'head', you get your element
  10. if (allChilds[i].id && allChilds[i].id == "head") {
  11. // Remove it from root
  12. root.removeChild(allChilds[i]);
  13. }
  14. }
  15. document.body.innerHTML = root.innerHTML;
  16. }
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
May 24th, 2009
0

Re: Parsing an HTML String

Hi itsjareds,

You can expand this function to be able to work with your needs.

javascript Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>Test Page</title>
  4. <script type="text/javascript">
  5. <!--
  6. var parseHTML;
  7. var root;
  8. var head;
  9. var body;
  10. var AJAXdummy;
  11.  
  12. parseHTML = function( html ) {
  13. head = document.getElementsByTagName("head");
  14. body = document.getElementsByTagName("body")[0];
  15. root = document.createElement("div");
  16. root.id = "newDiv";
  17. if ( root ) {
  18.  
  19. body.appendChild( root ); // Append Div element inside the body first.
  20.  
  21. body.removeChild( head[1] ); // Removing Second Header element inside the appended div.
  22.  
  23. root.innerHTML = html; // Injecting Contents from Ajax Request.
  24. }
  25. };
  26.  
  27. window.onload = function( ) {
  28. AJAXdummy = "<html><head><title>Test Parsing HTML</title></head>";
  29. AJAXdummy += "<body><h1>Hello Universe!</h1></body></html>";
  30. parseHTML( AJAXdummy );
  31. };
  32. //-->
  33. </script>
  34. </head>
  35. <body>
  36.  
  37. </body>
  38. </html>

The script was based on your provided function's and non-standard. Lite modification is required to obtained its maximum performance.

But i hope we've both claimed your needs...
Last edited by essential; May 24th, 2009 at 3:49 am.
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
May 24th, 2009
0

Re: Parsing an HTML String

Hi essential,

I think he do not want root to be appended to body.
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
May 24th, 2009
0

Re: Parsing an HTML String

Hi lucky,

I've make it just to run the entire function for testing. itsjareds is an experienced guy when it comes to javascript, im sure he can handle everything from there!

Anyways thanks for reminding me -
And good day to you...
Last edited by essential; May 24th, 2009 at 3:58 am.
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
May 24th, 2009
0

Re: Parsing an HTML String

Thanks for the help - all good answers. If I was not already working on the script, I would have used your first suggestion, LuckyChap, but I found that making a new tag called <header> and searching for it with getElementsByTagName also worked.

Your way seems probably more correct, as creating a <header> tag is XML-like, but at least I know that none of my pages will have duplicate issues with two divs with an id of header .

Here's the demo page if you're interested. It works correctly, and now I'm off to add RealSimpleHistory to allow back button support.
Last edited by itsjareds; May 24th, 2009 at 12:54 pm.
Reputation Points: 39
Solved Threads: 13
Junior Poster
itsjareds is offline Offline
103 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Image Preloader Script - Lite Version
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: How to display the details in right frame?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC