środa, stycznia 31, 2007

Ajax ciekawostki

  1. Możliwość behavior z IE udostępniona w FireFox
  2. http://rabaix.net/index.php/en/articles/issues_developing_ajax_libraries -
    1. If you want to send special characters, like non-ASCII characters, you need to send them encoded in UTF-8. In this purpose, you can use the encodeURIComponent function.

      var params = "money="+ encodeURIComponent('€ euro');
      [...] request code [...]

    2. Request Referrer

      When a query is sent to the server, the referrer value in the query may be empty. However for some reasons the script, which handles the request, may require this information. The XMLHttpRequest comes with a method to send specific headers. It is not possible to set the referrer value for security reasons, however any other name can be used, like "X-Referrer" (read the warning note).

      var xmlRequest = getNewXmlHttpRequest();
      xmlRequest.setRequestHeader("X-Referrer",document.location);

      On the server side you can access to the referrer with $_SERVER['X_REFERRER'].
    3. ResponseXML jak do niego dotrzeć? var firstnames = reponseXML.getElementsByTagName('firstname'); alert('The firstname is ' + firstnames[0]);
    4. W przeglądarkach nie IE jest możliwość serializacji:
    5. function getTextVersion(XMLnode) {
      var text;
      try {
      //serialization to string DOM Browser
      var serializer = new XMLSerializer();
      text = serializer.serializeToString(XMLnode);
      } catch(e) {
      // serialization of an XML to String (IE only)
      text = XMLnode.xml;
      }
      return text;
      }

      node.innerHTML = getTextVersion(xmlResponse)
    6. Nie można wykonać dosztukowanego kodu z responseText - when you append the responseText with innerHTML method, browsers ignore all JavaScript’s codes. Some browsers may transform the innerHTML: Internet Explore ignores the JavaScript code and Firefox may have some strange behaviors.
    7. Przy responseXML nie działa getElementById - po prostu go nie ma.
    8. Jest pewna możliwość wykonania kodu zwróconego w 'response'XML/Text:
      1. responseXML:
      2. function launchJavascriptFromXML(responseXML) {
        var scripts = responseXML.getElementsByTagName('script');
        var js = '';
        for(var s = 0; s < scripts.length; s++) {
        if(scripts[s].childNodes[0].nodeValue == null) continue;
        js += scripts[s].childNodes[0].nodeValue;
        }
        eval(js);
        }
      3. responseText:
      4. function launchJavascript(responseText) {
        // RegExp from prototype.sonio.net
        var ScriptFragment = '(?:<script.*?>)((n|.)*?)(?:</script>)';

        var match = new RegExp(ScriptFragment, 'img');
        var scripts = responseText.match(match);

        if(scripts) {
        var js = '';
        for(var s = 0; s < scripts.length; s++) {
        var match = new RegExp(ScriptFragment, 'im');
        js += scripts[s].match(match)[1];
        }
        eval(js);
        }
        }
    9. Uwagi: są pewne zastrzeżenia co do bezpieczeństwa takiego rozwiązania. Dostęp tak wykonywanego kodu jest globalny do istniejących już zmiennych i funkcji. Funkcje stworzone w tak ściągniętym poprzez response kawałku są lokalne -niewidoczne dla załadowanego do tego momentu już kodu.
    10. Można włączyć w kod AJAX inny kod AJAX (embedded)
      1. var AJAX_Objects = new Array();
        function registerAjaxObject(name, obj) {
        AJAX_Objects[name] = obj;
        }
      2. function onreadystatechange_embeded() {
        // get back the object
        if(AJAX_Objects["xmlRequestEmbeded"].readyState == 4) {
        alert('Hello I came from onreadystatechange_embeded function embeded on the xml');
        }
        }

        var xmlRequestEmbeded = getNewXmlHttpRequest();
        xmlRequestEmbeded.open("GET", SERVER + "/data/issues_developing_ajax_libraries/test.xml");
        xmlRequestEmbeded.onreadystatechange = onreadystatechange_embeded;
        registerAjaxObject("xmlRequestEmbeded", xmlRequestEmbeded);
        xmlRequestEmbeded.send(false);
      3. function getNewXmlHttpRequest() {
        var obj = false;
        try {
        obj = new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e) {
        try {
        obj = new ActiveXObject('Microsoft.XMLHTTP');
        } catch(e) {
        obj = new XMLHttpRequest();
        }
        }
        return obj;
        }
    11. Zasoby:
    12. Demo page of this article : http://rabaix.net/pub/issues_developing_ajax_libraries/demo.html
    13. Prototype library : http://prototype.conio.net/
    14. Wikipedia AJAX : http://en.wikipedia.org/wiki/AJAX
    15. Adaptive Path : http://www.adaptivepath.com/publications/essays/archives/000385.php
    16. Ajax Mistakes : http://sourcelabs.com/ajb/archives/2005/05/ajax_mistakes.html
    17. http://ajaxpatterns.org/On-Demand_Javascript

Brak komentarzy: