Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [xmlhttp/js] Kompatybilność przeglądarek..
Forum PHP.pl > Forum > XML, AJAX
katsuo
Hi!
Kod :
  1. <head>
  2. <script type="text/javascript">
  3. <!--
  4. function submitForm(){
  5. var xhr;
  6. if (window.XMLHttpRequest) {
  7. xhr = new XMLHttpRequest();
  8. alert('opera');
  9. }
  10. else if (window.ActiveXObject) {
  11. try {
  12. xhr= new ActiveXObject("Msxml2.XMLHTTP");
  13. }
  14. catch (e){
  15. try{
  16. xhr = new ActiveXObject("Microsoft.XMLHTTP");
  17. }
  18.  
  19. catch(e3){
  20. xhr=false;
  21. }
  22. }
  23. }
  24. xhr.open('POST','data.txt',true);
  25. xhr.send(null);
  26. xhr.onReadyStateChange=processReqChange();
  27. function processReqChange(){
  28. alert(xhr.readyState);
  29. if(xhr.readyState == 4){
  30. alert('test2');
  31. if(xhr.status == 200){
  32. var doc = xhr.responseTEXT;
  33.  
  34. alert('test3');
  35. document.getElementById('element1').innerHTML= doc; // Assign the content to the form
  36. alert( xhr.status);
  37. }
  38. else{
  39. document.getElementById('element1').innerHTML="Kod błędu: "+xhr.status;
  40. }
  41. }
  42. }
  43. }
  44. -->
  45. </script>
  46. </head>
  47. <body onLoad="submitForm();" >
  48. <div id="element1" ></div>
  49. </body>
  50. </html>


Zachowanie:
- IE 6.0: wyświetla treść pliku data.txt
- firefox 3.0.5: zamiast podmienić element1 na tresc- wyświetla 'undefinied'
- opera 9.63: nic nie wyświetla.

Problem pewnie banał, jednak byłbym bardzo wdzięcznym za pomoc:)

Pzdr.
ziqzaq
Troszeczkę przerobiony twój kod:
Kod
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type="text/javascript">
<!--
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
function submitForm(){
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert ("Your browser does not support AJAX!");
        return;
    }
    xmlHttp.onreadystatechange=processReqChange;
    xmlHttp.open("post","http://localhost/data.txt",true);
    xmlHttp.send(null);
}
function processReqChange() {
    //alert(xmlHttp.readyState);
    document.getElementById('e2').innerHTML=xmlHttp.readyState;
    if(xmlHttp.readyState == 4){
        //alert('test2');
        if(xmlHttp.status == 200){
            var doc = xmlHttp.responseText;
            //alert('test3');
            //alert(doc);
            document.getElementById('element1').innerHTML = doc; // Assign the content to the form
            //alert(xhr.status);
        }
        else{c
            document.getElementById('element1').innerHTML="Kod błędu: "+xmlHttp.status;
        }
    }
}
-->
</script>
</head>
<body>
<div id="element1" ></div>
<input type="button" value="Klik" onclick="submitForm();" />
<div>Stan: <span id="e2"></span></div>
</body>
</html>


Zwracam uwagę na:
Kod
xhr.onReadyStateChange=processReqChange();

nie przekazujemy wartości zwracanej z funkcji tylko wskaźnik do niej:
Kod
xhr.onReadyStateChange=processReqChange


Sprawdzone na Opera 9.60 i Firefox 3.0.4.

// Kod dałem do znaczników code bo html nie indentuje kodu.
katsuo
Teraz działa ładnie, dzięki. smile.gif
chyzio
a ja mam następujące pytanie zuwagi na którą funkcję kod musi być uruchamiany z serwera (kod oczywiście działa ale po uruchomieniu go z serwera)

  1. <script type="text/javascript">
  2. <!--
  3. function GetXmlHttpObject()
  4. {
  5. var xmlHttp=null;
  6. try
  7. {
  8. // Firefox, Opera 8.0+, Safari
  9. xmlHttp=new XMLHttpRequest();
  10. }
  11. catch (e)
  12. {
  13. // Internet Explorer
  14. try
  15. {
  16. xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  17. }
  18. catch (e)
  19. {
  20. xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  21. }
  22. }
  23. return xmlHttp;
  24. }
  25.  
  26. function submitForm($param){
  27. var wybor = $param;
  28. xmlHttp = GetXmlHttpObject();
  29. if (xmlHttp == null) {
  30. alert ("Your browser does not support AJAX!");
  31. return;
  32. }
  33. xmlHttp.onreadystatechange=processReqChange;
  34.  
  35.  
  36. switch (wybor) {
  37.  
  38. case 'home':
  39. xmlHttp.open("post","articles/home.html",true);
  40. document.getElementById('map').style.display='none';
  41. break
  42. case 'area':
  43. xmlHttp.open("post","articles/area.html",true);
  44. document.getElementById('map').style.display='block';
  45. break
  46. case 'tariffs':
  47. xmlHttp.open("post","articles/tariffs.html",true);
  48. document.getElementById('map').style.display='none';
  49. break
  50. case 'cars':
  51. xmlHttp.open("post","articles/cars.html",true);
  52. document.getElementById('map').style.display='none';
  53. break case 'contact':
  54. xmlHttp.open("post","articles/contact.html",true);
  55. document.getElementById('map').style.display='none';
  56. break
  57. case 'booking':
  58. xmlHttp.open("post","articles/booking.html",true);
  59. document.getElementById('map').style.display='none';
  60. break
  61. }
  62.  
  63. xmlHttp.send(null);
  64. }
  65.  
  66. function processReqChange() {
  67. //alert(xmlHttp.readyState);
  68. //document.getElementById('e2').innerHTML=xmlHttp.readyState;
  69. if(xmlHttp.readyState == 4){
  70. //alert('test2');
  71. if(xmlHttp.status == 200){
  72. var doc = xmlHttp.responseText;
  73. //alert('test3');
  74. //alert(doc);
  75. document.getElementById('element1').innerHTML = doc; // Assign the content to the form
  76. //alert(xhr.status);
  77. }
  78. else{c
  79. document.getElementById('element1').innerHTML="Kod bledu: "+xmlHttp.status;
  80. }
  81. }
  82. }
  83. -->
wookieb
xmlHttp.open i tego nie obejdziesz. Bo ajax załaduje ci strone tylko z serwera na ktorym znajduje sie witryna z tym kodem.
chyzio
wookieb, dziękuje za odpowiedź smile.gif Problem w tym że owy kod, działa na localhoscie, na nazwa.pl, i wielu innych serwerach a a na home.pl nie ! Mało tego dział wsparcia twierdzi iż owy kod nie wykonuje się na serwerze wiec to nie ich broszka. Więc to tylko potwierdza moją tezę iż nie maja racji.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.