luni, 22 septembrie 2008

AJAX - building an IM

Saptamana trecuta am redescoperit AJAX-ul si mi-a venit cheful sa fac o aplicatie complexa - IM(instant messenger). Asa ca am inceput sa structurez aplicatia, pornind de la un chat facut mai demult, adaugand un sistem de login. Toate bune si frumoase, dar mi-am dat seama ca userul poate sa se de-logheze de pe site prin buton sau prin inchiderea ferestrei.
Prima chestie usor de facut, a doua nu chiar asa de usor pentru ca nu prea mai lucrasem cu evenimentele ferestrei in JavaScript. Asa ca dupa cautari pe net am ajuns sa rezolv si a doua problema:

close.js
   1: function ajaxFunction(){
   2:     var ajaxRequest;
   3:  
   4:     try{
   5:         // Opera 8.0+, Firefox, Safari
   6:         ajaxRequest = new XMLHttpRequest();
   7:     } catch (e){
   8:         // Internet Explorer Browsers
   9:         try{
  10:             ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
  11:         } catch (e) {
  12:             try{
  13:                 ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
  14:             } catch (e){
  15:                 alert("Your browser sucks!");
  16:                 return false;
  17:             }
  18:         }
  19:     }
  20:  

  21:     ajaxRequest.open("GET", "test.php", true);
  22:     ajaxRequest.send(null);
  23: }
  24: window.onbeforeunload = function (evt) {
  25:        var message = ''If you press OK you will log off the IM';
  26:        if (typeof evt == 'undefined') {
  27:          evt = window.event;
  28:        }
  29:        if (evt) {
  30:          evt.returnValue = message;
  31:        }
  32:       return message;
  33:     }
  34:  
  35: window.onunload = ajaxFunction();

test.php //contine ce se intampla cand se inchida fereastra

close.php (sau close.html)

   1: <html>
   2: <head>
   3: <script language="JavaScript" type="text/javascript" src="close.js"></script>
   4: </head>
   5: </html>

The rest is in progress :P


LE: Windows Live Writer Beta e interesant :) – am folosit un plugin de code snippet si chiar arata bine.

Niciun comentariu: