Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [xmlhttp/js] Kompatybilność przeglądarek.., Kod działa tylko pod ie;/
katsuo
post 26.12.2008, 08:24:13
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
ziqzaq
post 26.12.2008, 13:28:54
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
katsuo
post 26.12.2008, 13:54:48
Post #3





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

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


Teraz działa ładnie, dzięki. smile.gif
Go to the top of the page
+Quote Post
chyzio
post 13.01.2009, 11:37:20
Post #4





Grupa: Zarejestrowani
Postów: 93
Pomógł: 0
Dołączył: 12.03.2006

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


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. -->
Go to the top of the page
+Quote Post
wookieb
post 13.01.2009, 12:40:05
Post #5





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




xmlHttp.open i tego nie obejdziesz. Bo ajax załaduje ci strone tylko z serwera na ktorym znajduje sie witryna z tym kodem.

Ten post edytował wookieb 13.01.2009, 12:41:05


--------------------
Go to the top of the page
+Quote Post
chyzio
post 13.01.2009, 18:09:43
Post #6





Grupa: Zarejestrowani
Postów: 93
Pomógł: 0
Dołączył: 12.03.2006

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


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.

Ten post edytował chyzio 13.01.2009, 18:10:09
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 27.06.2025 - 14:55