Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [JS] Funkcja od ajax'a
Eagle
post
Post #1





Grupa: Zarejestrowani
Postów: 170
Pomógł: 14
Dołączył: 16.03.2007

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


Witam. Dopiero zaczynam swoją przygodę z ajaxem.
Pogrzebałem trochę i znalazłem dość prosty przykład jak załadować kawałek xml'a http://developer.mozilla.org/en/AJAX/Getting_Started

Przerobiłem skrypt podany na stronie i wyszło mi coś takiego
Kod
function loadPage(pageUrl,htmlTag,xmlTag)
{
    if(xmlTag == undefined)
    {
        xmlTag = htmlTag;
    }

    var httpRequest;
    if (window.XMLHttpRequest)
    {
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType)
        {
            httpRequest.overrideMimeType('text/xml');
        }
    }
    else
    {
        if (window.ActiveXObject)
        {
            try
            {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e)
            {
                try
                {
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) {}
            }
        }
    }

    if (!httpRequest)
    {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    
    httpRequest.onreadystatechange = alertContents(httpRequest,htmlTag,xmlTag);
    httpRequest.open('GET', pageUrl, true);    
    httpRequest.send('');
}

function alertContents(httpRequest,htmlTag,xmlTag)
{
    if (httpRequest.readyState == 4)
    {
        if (httpRequest.status == 200)
        {
            var xmldoc = httpRequest.responseXML;
            var root_node = xmldoc.getElementsByTagName(xmlTag).item(0);
            var data = document.getElementById(htmlTag);
            data.innerHTML = root_node.firstChild.data;
        }
        else
        {
            alert('There was a problem with the request.');
        }
    }
}



Plik pole1.xml
  1. <?xml version="1.0" ?>
  2. <pole1>Jakiś tekst ble ble bel</pole1>


HTML:
  1. <head>
  2. <title>Moj pierwszy ajax!</title>
  3. <script type="text/javascript" src="jax.js"></script>
  4. </head>
  5. <body>
  6. <a href="" onClick="loadPage('pole1.xml','pole1','pole1')">Cos1</a>
  7. <br />
  8. <br />
  9. <div id="pole1"></div>
  10. </body>
  11. </html>


Jednak po kliknięciu na link cała strona się przeładowuje - nie wyświetla danych z pliku xml
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: 27.09.2025 - 05:43