SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member delamatrix's Avatar
    Join Date
    Dec 2004
    Location
    Chicago
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ajax Possibility?

    I would like to know if it's possible to load just a section by divs when using ajax. I currently have a working solution when I select an option from a form, and ajax loads the the index.cgi file into the div 'siteinfo'. My index.cgi file has a number of functions that are ran with if elseif conditions. But the problem is that ajax is loading the whole page, header and all. I would like to strip everything and just have ajax load and display the contents between the 'siteinfo' divs.

    Here's my ajax.js code:
    Code:
    var xmlhttp;
    
    function showSiteinfo(str)
    {
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
      {
      alert ("Browser does not support HTTP Request");
      return;
      }
    var url="index.cgi";
    url=url+"?site="+str;
    url=url+"&sid="+Math.random();
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    
    function stateChanged()
    {
    document.getElementById("siteinfo").innerHTML = "<p align='center'><img src=bigrotation2.gif></p>";
    if (xmlhttp.readyState==4)
    {
    document.getElementById("siteinfo").innerHTML=xmlhttp.responseText;
    }
    }
    
    function GetXmlHttpObject()
    {
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
    }
    Here's some code snippet from index.cgi:
    Code:
    <select name='site' onchange='showSiteinfo(this.value)'>
    Is what I'm trying to accomplish possible?

    Thanks,

    Golden

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,898
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Instead of javascript stripping away everything thing it doesn't need it would be better to let the server only send the info javascript needs.

    - Less bandwidth
    - Less server processing time
    - Less client processing time


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
  •