Processing Processing

JavaScript Anyone? This is a weird problem.. Please Help!

rated by 0 users
This post has 2 Replies | 1 Follower

Top 10 Contributor
Joined: Jan 2010
379 Posts
Softec Committee - Web Team
Softec Groups - CEO Roundtables Membership
Softec Sponsorship - Partners
Justin Couto Wed, Feb 17 2010 6:49 PM

I am using jQuery, but this problem is not specific to jQuery as I have tried it using straight javascript and the problem still occurs.  What I am trying to do is grab the contents of a DIV that contains XHTML.  I am then taking that string of XHTML (The Contents of the DIV) and adding it to a textarea.  This works just fine.  The problem is that the selection of XHTML content appears to be converting it to HTML.

Example:
<img /> becomes <img>
<br /> becomes <br>

I need the HTML to be XHTML compliant.  Has anyone ran into this before?

Thanks in Advanced! 

Justin Couto

Couto Solutions CEO
4115 Broad Street, Suite B-5, San Luis Obispo, CA 93401
Phone: (805) 540-3335 | Web: www.CoutoSolutions.com

Top 75 Contributor
Joined: Jan 2010
1 Posts
Softec Committee - Web Team
Softec Sponsorship - Individual Affiliate

I found this online but haven't tested it. Unfortunately I couldn't find any standards compliant way of doing this, but maybe the XMLSerializer object will become a standard. Of course, this works for any DOM element, including responseXML out of XmlHttpRequest.


function xml2Str(xmlNode) {
   try {
      // Gecko- and Webkit-based browsers (Firefox, Chrome), Opera.
      return (new XMLSerializer()).serializeToString(xmlNode);
  }
  catch (e) {
     try {
        // Internet Explorer.
        return xmlNode.xml;
     }
     catch (e) {  
        //Other browsers without XML Serializer
        alert('Xmlserializer not supported');
     }
   }
   return false;
}

Source:http://stackoverflow.com/questions/43455/how-do-i-serialize-a-dom-to-xml-text-using-javascript-in-a-cross-browser-way

 

Hans-Peter Klett

Couto Solutions Development Team
4115 Broad Street, Suite B-5, San Luis Obispo, CA 93401
Phone: (805) 540-3335 | Web: www.CoutoSolutions.com

Top 10 Contributor
Joined: Jan 2010
379 Posts
Softec Committee - Web Team
Softec Groups - CEO Roundtables Membership
Softec Sponsorship - Partners

I tired using this method and I was running into issue with it.  I ended us using this script on codeplex that solved the problem for me.  It was designed to do exactly what I need.

Here is a URL to the script:
http://xhtmljs.codeplex.com/releases/view/12048

To use it with jQuery you simply do:
var xhtmlContent = $("#content").xhtml();

The above example would return a string of the exact content you would expect.

Thanks for the help,

Justin Couto

Couto Solutions CEO
4115 Broad Street, Suite B-5, San Luis Obispo, CA 93401
Phone: (805) 540-3335 | Web: www.CoutoSolutions.com

Page 1 of 1 (3 items) | View All | Print All | RSS
The convenience of knowing what topics you have not read is provided by Couto Solutions’ Post Tracker for Telligent Community Forums.