Parsing an HTML String

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: May 2009
Posts: 97
Reputation: itsjareds is an unknown quantity at this point 
Solved Threads: 12
itsjareds's Avatar
itsjareds itsjareds is offline Offline
Junior Poster in Training

Parsing an HTML String

 
0
  #1
May 24th, 2009
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:
  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.
Mark your post as Solved if you've been helped!

I learn by helping others learn.
If you find one of my posts helpful, don't be afraid to give me a reputation bump :D
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: Parsing an HTML String

 
1
  #2
May 24th, 2009
getElementById() only works with document. To parse your HTML can use childNodes;

Modifying your code all little bit:

  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. }
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Parsing an HTML String

 
0
  #3
May 24th, 2009
Hi itsjareds,

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

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: Parsing an HTML String

 
0
  #4
May 24th, 2009
Hi essential,

I think he do not want root to be appended to body.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Parsing an HTML String

 
0
  #5
May 24th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 97
Reputation: itsjareds is an unknown quantity at this point 
Solved Threads: 12
itsjareds's Avatar
itsjareds itsjareds is offline Offline
Junior Poster in Training

Re: Parsing an HTML String

 
0
  #6
May 24th, 2009
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.
Mark your post as Solved if you've been helped!

I learn by helping others learn.
If you find one of my posts helpful, don't be afraid to give me a reputation bump :D
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC