views:

1569

answers:

1

Before someone said that I did not read I may say that I read almost everything linked with my question. But I couldn't find my answer. So, I have a simple AJAX script that loads my external file inside predefined div. This is the code of those script:

    function loadTwitter()
  {
  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 Don't Support AJAX!");
        return false;
        }
      }
    }
    xmlHttp.onreadystatechange=function()
      {
      if(xmlHttp.readyState==4)
        {
        document.getElementById("column_twitter").innerHTML=xmlHttp.responseText;
        }
      }
    xmlHttp.open("GET","../includes/home/twitter.php",true);
    xmlHttp.send(null);
  }

It works just fine in everyone browser that I test (FF, Opera, Chrome, Safari), but inside IE7 don't want to inject my external php file into predefined div. It always stays the default text that I wright inside div... And I think that the problem is in this row:

document.getElementById("column_twitter").innerHTML=xmlHttp.responseText;

So, any suggestions how to fix this for IE (7 and above)?

+5  A: 

I think you'd be better off using a javascript framework such as jQuery that allows you to concentrate on getting your features implemented rather than browser compatibility and low level network interaction. Using jQuery you could simply do:

<script type="text/javascript"
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;
</script>
<script type="text/javascript">

$.get( '../includes/home/twitter.php', function(data) {
     $('#column_twitter').html( data );
});

</script>
tvanfosson
+1'd, even shorter would be $('#column_twitter').load('../includes/home/twitter.php');
karim79
Thanks, EXACTLY what I needed! Clean and Simple! Thanks a lot!!!
Spoonk
@karim79 - yeah, that's definitely shorter. I hardly ever use load though, because usually I'm passing data back or I want to do something more complicated after I get the data.
tvanfosson