FRIHOSTFORUMSSEARCHFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Is there a simple way to convert a DOM back to HTML?

 


DoctorBeaver
I've started getting into AJAX and familiarising myself with the DOM. I understand the tree schema (I've used similar myself in language parsing) and how to modify it using JavaScipt.

What I want to do is to allow my members to re-position elements on a few of the pages. At the moment I'm saving any changes in an external text file and, using php, creating the page "on the fly" before it is sent to the browser. This means I have to start with a template and apply the changes each time the page is loaded. That is slow, cumbersome, error-prone and not easy to change if I want to add more functionality or the member changes things again (multiple change logs!).

Is there a simple way of converting the modified DOM back to HTML so that I can simply save the new version of the page? I really don't want to go to the hassle of writing my own parser to do it.

Incidentally, the pages concerned are pure HTML. There is no JavaScript or php involved.
jmraker
Most web browsers let you use domElement.innerHTML which is the HTML of it's children which can also be used to add/edit/delete the DOM by changing it's value. If you change it, you're re-creating the HTML so you'll erase any event handlers if there are some in the children.

Code:
elem = document.getElementById("a");
alert(elem.innerHTML);
DoctorBeaver
jmraker - so can I just save domElement.innerHTML as the new page?
Marcuzzo
Quote:
New postPosted: Thu Dec 11, 2008 6:20 pm Post subject: Report this post Reply with quote
jmraker - so can I just save domElement.innerHTML as the new page?


yes and no.

You can modify the contents of the document at runtime using DOM, but when the page is reloaded only the hard coded html will be loaded into the memory.

the XML object has a method .save(); if you are looking at an XML approach, but this will only work locally if I remember correctly.

with server Sided scripting you could send the new html code to a script which saves the new content to a file on the server. but I wouldn't use it as this could be exploited.


and
Quote:
Code:

elem = document.getElementById("a");
alert(elem.innerHTML);


this will only get the innerHTML of an element with id='a'
like <a href="http://www.google.be" id="a">google</a>

I think you meant:

Code:
var links = document.getElementsByTagName("a");
for (var i = 0;i<links.length;i++){
 alert(links[i].innerHTML);
}
Related topics

How can I convert a DOM back to HTML?
How to build simple PHP upload form
simple webserver on cable line.
Want to know if LOVE is real?
Alternate Realities

Why partition your hard disk
Fujitsu Siemens Laptop
How is it done?
MD5...
Should I whack them up?

AJAX tutorial [2nd part now updated]
The Middle East Conflict
How dose time slow, at Post-lightspeed?
Logging in a php game
Format and ... ups ...
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.