Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [xmlhttp/js] Kompatybilność przeglądarek.., Kod działa tylko pod ie;/
katsuo
post
Post #1





Grupa: Zarejestrowani
Postów: 18
Pomógł: 1
Dołączył: 1.05.2007

Ostrzeżenie: (0%)
-----


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.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
ziqzaq
post
Post #2





Grupa: Zarejestrowani
Postów: 428
Pomógł: 128
Dołączył: 17.06.2007

Ostrzeżenie: (0%)
-----


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.

Ten post edytował ziqzaq 26.12.2008, 13:48:18
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 26.09.2025 - 10:17