Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> xmlHttp.onreadystatechange - przypisanie funkcji z parametru
nitek
post
Post #1





Grupa: Zarejestrowani
Postów: 209
Pomógł: 37
Dołączył: 27.02.2008
Skąd: Kwidzyn

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


witam,

stosunkowo niedawno zacząłem zabawę z Ajaxem i mam pytanie, czy dozwolone jest w jakiś sposób coś takiego:

Kod
xmlHttp.onreadystatechange=fun;

zamiast
Kod
xmlHttp.onreadystatechange=stateChanged;


gdzie fun, to parametr z funkcji. przy wywoływaniu change_content chciałbym dać w parametrze, która z funkcji ma zostać wykonana - stateChanged(), czy validate_temat().

Z góry dzięki za jakieś wskazówki.
Kod skryptu poniżej, funkcję wywołuje np.
Kod
onclick="change_content('main.php','img/01_main.jpg','stateChanged'); return false;">



Kod
var xmlHttp;
var img_strona;

function change_content(url,img_str,fun)
  {
  xmlHttp=GetXmlHttpObject();
    
  if (xmlHttp==null)
   {
   alert ("Browser does not support HTTP Request");
   return;
   }
  
  img_strona=img_str;
    
    var params = "";
  xmlHttp.onreadystatechange=stateChanged;
  //xmlHttp.onreadystatechange=fun;

  xmlHttp.open("POST",url,true);
    
  xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader("Content-length", params.length);
  xmlHttp.setRequestHeader("Connection", "close");

    
  xmlHttp.send(params);
  }

    
    
function stateChanged()
  {
    //alert('jest');
  if (xmlHttp.readyState==1)
   {
   document.getElementById("content_right").innerHTML='Proszę czekać...';
   document.body.style.cursor = "wait";
   }
  
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
   {
   document.getElementById("content_right").innerHTML=xmlHttp.responseText;
   document.body.style.cursor = "default";
     document.getElementById("top_page_img").innerHTML="<img src='"+img_strona+"' alt='>";
   }
  }

    
function validate_temat()
  {
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
   {
   document.getElementById("temat").style.color="red";
   }
  }
    
    
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;
  }


Ten post edytował nitek 16.03.2009, 18:00:42


--------------------
Go to the top of the page
+Quote Post

Posty w temacie


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 Aktualny czas: 20.08.2025 - 15:51