Ajax Talk - Asynchronous javascript and xml discussion board
 FAQ   Search    Usergroups      Register   Profile   Check your messages   Log in 
Signup now to enjoy a range of free membership advantages !!
Ajax Talk - Moderators Wanted
Javascript can't get html code from a xml request

 
Post new topic   Reply to topic    Ajaxtalk.com Forum Index -> AJAX Help
View previous topic :: View next topic  
Author Message
rReLmy
New one
New one


Joined: 17 Oct 2007
Posts: 3
Location: basel

PostPosted: Wed Oct 17, 2007 8:53 pm    Post subject: Javascript can't get html code from a xml request Reply with quote

Hi

I've got a problem to become html from a xml answer.
What comes out is only the part before the first html Tag.

I created my Script from this 2 Tutorials
http://developer.mozilla.org/en/docs/AJAX:Getting_Started
http://www.quirksmode.org/blog/archives/2005/12/the_ajax_respon.html *XML part*

why does the js cut all after the first html Tag?
I can't understand it...

Here is the sample page http://www.rrelmy.ch/ajax/xml
P.S. to see the answer of the Script use Firebug FF Extension.

the js
Code:
<script type="text/javascript">
function makeRequest(site) {
        var httpRequest;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // See note below about this line
            }
        }
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e) {
                           try {
                                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                               }
                             catch (e) {}
                          }
                                       }

        if (!httpRequest) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        httpRequest.onreadystatechange = function() { alertContents(httpRequest, site); };
        httpRequest.open('GET', 'ajax.php?site='+site, true);
        httpRequest.send('');

    }

    function alertContents(httpRequest, site) {

        try {
            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
                    //alert(httpRequest.responseText);
                    //document.getElementById('content').innerHTML = httpRequest.responseText;
                    gen_site(httpRequest, site);
                } else {
                    alert('There was a problem with the request.');
                }
            }
        }
        catch( e ) {
            alert('Caught Exception: ' + e.description);
        }

    }
    function gen_site(xml, site) {

   var html = xml.responseXML.getElementsByTagName('replace');
   
   //alert(html.length);
   
   for (var i=0;i<html.length;i++)
   {
      //alert(getNodeValue(html[i],'id'));
      document.getElementById(getNodeValue(html[i],'id')).innerHTML = getNodeValue(html[i],'html');
   }

       set_navi(site);
    }

    function set_navi(site) {
       document.getElementById('n_home').setAttribute("class", "", 0);
       document.getElementById('n_scripts').setAttribute("class", "", 0);
       document.getElementById('n_'+site).setAttribute("class", "on", 0);
    }

function getNodeValue(obj,tag)
{
   return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
   //return obj.getElementsByTagName(tag)[0].firstChild.innerHTML;
}

function isset(varname)  {
  if(typeof( window[ varname ] ) != "undefined") return true;
  else return false;
}

</script>


sample ajax answer
Code:
<?xml version="2.0" ?>
<root>
<replace>
   <id>content</id>
   <html>The Content <p>wont be shown, bliblablub</p></html>
</replace>
<replace>
   <id>panel</id>
   <html>Something Other<b>Blub</b></html>
</replace>
</root>
Back to top
View user's profile Send private message Visit poster's website
Jesdisciple
Frequent Poster
Frequent Poster


Joined: 20 Oct 2007
Posts: 36
Location: Tejas

PostPosted: Sun Oct 21, 2007 1:23 am    Post subject: Reply with quote

Technically, HTML isn't XML; it's SGML (as is XML). I'm an AJAX newb myself so I don't know for sure, but that may be why it's prohibited as a value for responseXML.

But, if that is the reason, XML isn't so extensible as I thought.
_________________
In ALL things, strive for ><>,
Chris
Back to top
View user's profile Send private message Visit poster's website AIM Address MSN Messenger
rReLmy
New one
New one


Joined: 17 Oct 2007
Posts: 3
Location: basel

PostPosted: Wed Oct 24, 2007 9:25 pm    Post subject: Reply with quote

Thx for your answer.

After Hours of searching and trying i've found the solution!
Really Smile

You have to put the CDATA thing around the "html"

A sample
Code:

<root>
<replace>
   <id>content</id>
   <html><![CDATA[The Content <p>wont be shown, bliblablub</p>]]></html>
</replace>
<replace>
   <id>panel</id>
   <html><![CDATA[Something Other<b>Blub</b>]]></html>
</replace>
</root>


The JS Code in my first Post should work with it.
Back to top
View user's profile Send private message Visit poster's website
Jesdisciple
Frequent Poster
Frequent Poster


Joined: 20 Oct 2007
Posts: 36
Location: Tejas

PostPosted: Wed Oct 24, 2007 9:33 pm    Post subject: Oh, yeah... Reply with quote

Now that you mention it, I remember reading that here (link). This just goes to show how long ago that was and how much I've used it (none).
_________________
In ALL things, strive for ><>,
Chris
Back to top
View user's profile Send private message Visit poster's website AIM Address MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Ajaxtalk.com Forum Index -> AJAX Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
 Advertisements
 XML & Copyright Notice 
    


Powered by phpBB © 2001, 2005 phpBB Group

What is according to Wikipedia: Ajax Web2.0 XML E4X


Link to our site:

  


del.icio.us digg spurl wists simpy newsvine blinklist furl blogmarks yahoo! myweb smarking ma.gnolia segnalo reddit fark technorati cosmos


Normal Bookmark


Partner Button Links:
Codecrunch.com: Webmaster Tutorials    One Ajax


Partner Text Links:
quomon | web20log | ajaxmatters | tableless | 456bereastreet | music charts | gift ideas | competitions | free xbox 360 | link trade ?


Credits:
it consultancy & project management lead generation zoekmachine optimalisatie steal these buttons seo expert