SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ajax code working on FF and Safari but not on IE or Opera

    Hi,

    I'm kind of new to ajax and i have this problem where a simple script works on FF and Safari, but not on IE (6 or 7) or Opera.

    Its a simple script that displays an updated timestamp every second.

    here is the code:

    Code:
    <?php
    header('Pragma: no-cache');
    ?>
    
    <head>
    <script type="text/javascript">
    var page = "time.php";
    function ajax(url,target)
     {
        // native XMLHttpRequest object
       document.getElementById(target).innerHTML = 'sending...';
       if (window.XMLHttpRequest) {
           req = new XMLHttpRequest();
           req.onreadystatechange = function() {ajaxDone(target);};
           req.open("GET", url, true);
           req.send(null);
       // IE/Windows ActiveX version
       } else if (window.ActiveXObject) {
           req = new ActiveXObject("Microsoft.XMLHTTP");
           if (req) {
               req.onreadystatechange = function() {ajaxDone(target);};
               req.open("GET", url, true);
               req.send();
           }
       }
    		   setTimeout("ajax(page,'scriptoutput')", 1000);
    }
    
    function ajaxDone(target) {
    // only if req is "loaded"
    if (req.readyState == 4) {
    // only if "OK"
    if (req.status == 200 || req.status == 304) {
    results = req.responseText;
    document.getElementById(target).innerHTML = results;
    } else {
    document.getElementById(target).innerHTML="ajax error:\n" +
    req.statusText;
    }
    }
    }
    </script>
    </head>
    <body onload="ajax(page,'scriptoutput')">
    <p>Current Server date & time:<br />
    <span id="scriptoutput"></span></p>
    </body>
    and time.php :

    Code:
    <?php
    echo time();
    ?>
    could use some help here,

    Thanks!

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no point in using AJAX to fetch content into a page when the page first loads when you can simply echo that content to the browser when the page is rendered. That said, this function should create an XMLHttpRequest object in all browsers.

    Code:
    // Function to create an XMLHttpRequest object.
    
    function ajax() {
     var xhr = false;
     try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
     }
     catch (e) {
      try {
       xhr = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
       xhr = false;
      }
     }
     if (!xhr && (typeof XMLHttpRequest != "undefined")) {
      try {
       xhr = new XMLHttpRequest();
      }
      catch (e) {
       xhr = false;
      }
     }
     if (!xhr && window.createRequest) {
      try {
       xhr = window.createRequest();
      }
      catch (e) {
       xhr = false;
      }
     }
     return xhr;
    }
    
    // Response handler (see comment below).
    
    function handleResponse() {
     if (req.readyState == 4) {
      if (req.status == 200 || req.status == 304) document.getElementById('scriptoutput').innerHTML = req.responseText;
      ...
     }
    }
    
    // example usage
    
    var req = ajax();
    // Note that only the name of the handler function should be used here.
    // Thus, your handler function should not take any arguments.
    req.onreadystatechange = handleResponse;
    // IE and some other browsers may not perform the request unless a
    // relative URL is supplied to req.open, so do this:
    req.open('GET','/folder1/folder2/time.php',true);
    req.send();

  3. #3
    SitePoint Member
    Join Date
    Apr 2008
    Location
    Seattle, WA
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure what you went through all that jsut to render a timestamp. You could just put the <?php echo time();?> in the page itself and be done with it. If it's not a .php page, then you can also use javascript without AJAX to display the time

    <script type="text/javascript">
    <!-- hide from old browsers
    var curDateTime = new Date()
    document.write(curDateTime)
    //-->
    </script>

  4. #4
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I solved this thread in the DigitalPoint forum.

    http://forums.digitalpoint.com/showthread.php?t=890468

    Its pretty annoying when people post their problem in two different forums at the same time.
    mmj

  5. #5
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by M-M-J View Post
    Its pretty annoying when people post their problem in two different forums at the same time.
    Especially when they don't give it time for others to reply before re-posting.


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
  •