homepage Welcome to WebmasterWorld Guest from 54.234.2.88
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Access full page source after changes applied
with DOM scripting
rover




msg:3163913
 5:09 pm on Nov 21, 2006 (gmt 0)

I need to add a span to all text nodes in an html document, and found a basic solution that's included below.

This works fine and it will display the page on the fly with the spans functioning, but the problem is that I need to somehow output all of the source html with the actual span tags included.

Is there any way with DOM scripting that I can access/output the full source with the changes were made dynamically (and don't show on the actual html source)?

I need to get it into a variable or in some other way, but I don't know if this is even possible...

<HTML>
<HEAD>
<STYLE>
.js { color: white; background-color: orange; font-weight: bold; }
</STYLE>
<SCRIPT>
function spanAllTextNodes (styleClass, element) {
if (!element)
element = document.body;
for (var c = 0; c < element.childNodes.length; c++)
if (element.childNodes[c].nodeType == 3) {
var span = document.createElement('SPAN');
span.className = styleClass;
var textNode = element.replaceChild(span, element.childNodes[c]);
span.appendChild(textNode);
}
else
spanAllTextNodes(styleClass, element.childNodes[c]);
}
</SCRIPT>
</HEAD>
<BODY ONLOAD="spanAllTextNodes('js');">
<A HREF="here">
Some Text
</A>
<P>
More Text
</P>
<TABLE>
<TR>
<TD>
More Text
</TD>
</TR>
</TABLE>
</BODY>
</HTML>

 

rover




msg:3163993
 6:11 pm on Nov 21, 2006 (gmt 0)

I poked around with the Firefox DOM Inspector, and now see that I can access the contents of the page body with:

document.body.innerHTML

and that shows the body HTML with the span tags as dynamically placed by the javascript...

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved