www.webdeveloper.com
+ Reply to Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2012
    Posts
    23

    Trying to wrap my mind around AJAX...not getting far.

    I have a blog i am creating from scratch. I have the html page i want to publish the blog to, the javascript file that is going to be publishing the blog, and the xml file that contains the blog posts. I have looked at all of the information on the w3schools website and i cannot find what is wrong with the script. The script has a function named Blog() in it that i want activated by a button. Here is my code that i have.

    Here is the html code i have.


    <div id="blog">
    <script type="text/javascript" scr="blog.js"></script>
    <button type="button" onclick="Blog()">click me</button>
    </div>


    Here is the code from blog.js

    function Blog()
    {
    var XHR;
    if(window.XMLHttpRequest)
    {
    XHR = new XMLHttpRequest();
    }
    else
    {
    XHR = new ActiveXObject("Microsoft.XMLHTTP");
    }

    XHR.onReadyStateChange = function()
    {
    if(XHR.readyState == 4 && XHR.status == 200)
    {
    var xmldom = XHR.responseXML;
    var output = "";
    var items = xmldom.getElementsByTagName("item");

    for(var i = 0;i < items.length;i++)
    {
    output = xmldom;
    }

    document.getElementById("blog").innerHTML = output;
    }
    }

    XHR.open("GET", "blog.xml", true);

    XHR.send();
    }


    Here is the code from the blog.xml file.

    <?xml version="1.0" encoding="UTF-8"?>

    <rss version="2.0">

    <channel>
    <item>
    <title>hello</title>

    <link>www.google.com</link>

    <description>world</description>
    </item>
    <item>
    <title>hello</title>

    <link>www.google.com</link>

    <description>world</description>
    </item>
    <item>
    <title>hello</title>

    <link>www.google.com</link>

    <description>world</description>
    </item>
    <title>
    Unified Feed
    </title>

    <category>
    Business
    </category>

    <language>
    US-en
    </language>

    <ttl>
    60
    </ttl>
    </channel>

    </rss>

    Would love some help with this .

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,059
    Code:
    for(var i = 0;i < items.length;i++)
      {
        output = xmldom;    
      }
    Code:
    for(var i = 0;i < items.length;i++)
    {
      output += xmldom.innerHTML;    
    }
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Aug 2012
    Posts
    23
    Quote Originally Posted by Logic Ali View Post
    Code:
    for(var i = 0;i < items.length;i++)
    {
      output += xmldom.innerHTML;    
    }
    xml documents do not have the innerHTML property. can somebody explain this. also i change all of the code in the if(xhr.status == 4) loop to:

    alert(xmldom.documentElement.nodeName);

    and for some reason it does nothing. like the file doesn't respond at all.

  4. #4
    Join Date
    Aug 2012
    Location
    Belgium
    Posts
    66
    <div id="blog">
    <script type="text/javascript" scr="blog.js"></script>
    <button type="button" onclick="Blog()">click me</button>
    </div>

    try it with src="blog.js"^^

  5. #5
    Join Date
    Aug 2012
    Posts
    23
    Quote Originally Posted by Gudlife View Post
    <div id="blog">
    <script type="text/javascript" scr="blog.js"></script>
    <button type="button" onclick="Blog()">click me</button>
    </div>

    try it with src="blog.js"^^
    i tried to make my script an inline script and that didn't work. i also tried this and it didn't work :\.

  6. #6
    Join Date
    Aug 2012
    Location
    Belgium
    Posts
    66
    Is your site online?
    Maybe I could have a look that way.

  7. #7
    Join Date
    Aug 2012
    Posts
    23
    i sent you the url of my site in a private message.

  8. #8
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,059
    <script type="text/javascript" scr="blog.js"></script>
    As you have been told already but may have missed, that should be src

    Clearly you aren't even using the error console.

    Code:
    <div id="blog">
    <script type="text/javascript" src="blog.js"></script>
    <button type="button" onclick="Blog()">click me</button>
    </div>
    Above you have contained your script and button inside an element whose content you are about to wipe-out.

    Code:
    <div id="blog">  </div>
     <script type="text/javascript" src="blog.js"></script>
     <button type="button" onclick="Blog()">click me</button>
    Code:
    XHR.onReadyStateChange =
    Event handlers are written all lowercase.


    This will extract your content:
    Code:
    function Blog()
    {
    var XHR;
    if(window.XMLHttpRequest)
    {
    XHR = new XMLHttpRequest();
    }
    else
    {
    XHR = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    XHR.onreadystatechange = function()
    {
      if(XHR.readyState == 4 && XHR.status == 200)
      {
        var xmldom = XHR.responseXML;
        var output = "";
        var items = xmldom.getElementsByTagName("item");
           
        for( var i = 0; i < items.length; i++ )
        { 
          var cn = items[ i ].getElementsByTagName( '*' ),
              cnLen = cn.length;            
                
          for( var j = 0; j < cnLen; j++ )
            output += cn[ j ].firstChild.nodeValue + ' ';
                  
          output += '<br>';  
        }
            
        document.getElementById("blog").innerHTML = output;
      }
    }
    
    XHR.open("GET", "blog.xml", true);
    
    XHR.send();
    }
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



Recent Articles