Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Ajax

  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Ajax

    I'm attempting to display content from another html page within a div, but I can't get it to work. Can somebody help me out?
    Code:
    var xhr = null;
    var url="";
    // Attempt to create the XMLHttpObject
    function getXMLHttpRequest() {
        var xmlHttpObj = null;
        if (window.XMLHttpRequest) { 
            xmlHttpObj = new XMLHttpRequest();
        } else if (window.ActiveXObject){ 
            xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
        }
    
        return xmlHttpObj;
    }
    
    function bear() {
        xhr = getXMLHttpRequest(); 
            xhr.open("GET","bear.htm", true);
            xhr.onreadystatechange = handleResponse();
            xhr.send();
    }
    
    function handleResponse() {
        document.getElementById('div').innerHTML=xhr.responseXML;
    }

  • #2
    Regular Coder
    Join Date
    Mar 2011
    Posts
    148
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Hi,
    Try this code:
    Code:
    <div id="divid"> </div>
    <script type="text/javascript"><!--
    // from www.coursesweb.net/ajax
    // create the XMLHttpRequest object, according browser
    function get_XmlHttp() {
      // create the variable that will contain the instance of the XMLHttpRequest object (initially with null value)
      var xmlHttp = null;
    
      if(window.XMLHttpRequest) {		// for Forefox, IE7+, Opera, Safari, ...
        xmlHttp = new XMLHttpRequest();
      }
      else if(window.ActiveXObject) {	// for Internet Explorer 5 or 6
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
    
      return xmlHttp;
    }
    
    // sends data to a php file, via GET, and displays the received answer
    function ajaxrequest(tagID) {
      var request =  get_XmlHttp();		// call the function for the XMLHttpRequest instance
    
      request.open("GET", 'bear.htm', true);			// define the request
      request.send(null);		// sends data
    
      // Check request status
      // If the response is received completely, will be transferred to the HTML tag with tagID
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          document.getElementById(tagID).innerHTML = request.responseText;
        }
      }
    }
    
    ajaxrequest('divid');            // calls the ajax function
    --></script>


  •  

    Posting Permissions

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