SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OnClick ajax only works once in IE and Opera

    Hi, I use ajax to change the capcha in a form. For some reason it only works once in IE and Opera. In other words the image only changes once.

    Maybe its the ActiveX used for XMLHttp?

    Anyway around this?

    Thanks.

  2. #2
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It would help tremendously if you pasted some code.

  3. #3
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is my JS.
    PHP Code:
    <script type="text/javascript">
    function 
    ajaxFunction()
    {
    var 
    xmlHttp;
    try
      {
      
    // Firefox, Opera 8.0+, Safari
      
    xmlHttp=new XMLHttpRequest();
      }
    catch (
    e)
      {
      
    // Internet Explorer
      
    try
        {
        
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
      catch (
    e)
        {
        try
          {
          
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        catch (
    e)
          {
          
    alert("Your browser does not support AJAX!");
          return 
    false;
          }
        }
      }
      
    xmlHttp.onreadystatechange=function()
        {
        if(
    xmlHttp.readyState==4)
          {
          var 
    data xmlHttp.responseText.split("\n");
          
    document.Register.secimg.src=data[0];
          
    document.Register.sessid.value=data[1];
          }
        }
      
    xmlHttp.open("GET","scajax.php",true);
      
    xmlHttp.send(null);
      }
    </script> 

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like the response you get from the AJAX call is being cached by your browser. This is only an issue with the GET verb (POST doesn't cache in IE).

    To fix this, either use POST or append a random-ish querystring to the url:
    Code:
    xmlHttp.open("GET","scajax.php?r=" + Math.random(),true);  
    xmlHttp.send(null);

  5. #5
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the Math.random() function? I've heard about it but don't actually know what it is?

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,712
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    It creates a random number between 0.0 and 1.0
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And do I need to do anything with the number or will adding it as shown above solve the problem?

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,712
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    You just need to add it as above. The reason for putting a random number on it is to ensure that IE doesn't use a cached version.

    Another common solution that has also been used it to put the timestamp on the end. The timestamp may work better, but a random number should do the job just fine.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.


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
  •