Jump to content
php.lv forumi

AJAX


labaiss

Recommended Posts

Tā kā tā lapa mazliet bremzējas, tad iekopēju skriptu, lai nav jāgaida 2 min :) Ceru, ka tas ir tas, ko meklēji

 

 

INDEX.HTML

 

<html>

<head>

<title>AJAX Tutorial</title><script language="JavaScript" src="ajax.js"></script>

<script language="JavaScript">

function doSomethingWithData(str)

{

document.getElementById('MyID').innerHTML = str;

}

</script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><style type="text/css">

<!--

body,td,th {

font-family: Verdana, Arial, Helvetica, sans-serif;

font-size: 11px;

color: #666666;

}

-->

</style></head>

<body>

<b>AJAX Tutorial: Example 1</b><br />

This example shows how to dynamically retrieve data from a JavaScript file.<br /><br />

<a href="#" onClick="loadScript('Data1.js')">Data File 1</a><br />

<a href="#" onClick="loadScript('Data2.js')">Data File 2</a><br />

<a href="#" onClick="loadScript('Data3.js')">Data File 3</a><br />

<br />

<div id="MyID"> </div>

</body>

</html>

 

 

AJAX.JS

 

 

function loadScript(scriptURL)

{

var newScript = document.createElement("script");

newScript.src = scriptURL;

document.body.appendChild(newScript);

}

 

function loadData(URL)

{

// Create the XML request

xmlReq = null;

if(window.XMLHttpRequest) xmlReq = new XMLHttpRequest();

else if(window.ActiveXObject) xmlReq = new ActiveXObject("Microsoft.XMLHTTP");

if(xmlReq==null) return; // Failed to create the request

 

// Anonymous function to handle changed request states

xmlReq.onreadystatechange = function()

{

switch(xmlReq.readyState)

{

case 0: // Uninitialized

break;

case 1: // Loading

break;

case 2: // Loaded

break;

case 3: // Interactive

break;

case 4: // Done!

// Retrieve the data between the <quote> tags

doSomethingWithData(xmlReq.responseXML.getElementsByTagName('quote')[0].firstChild.data);

break;

default:

break;

}

}

 

// Make the request

xmlReq.open ('GET', URL, true);

xmlReq.send (null);

}

 

 

 

Data1.JS

 

doSomethingWithData("Text Data 1 - Hello World");

 

Data2.JS

 

doSomethingWithData("Text Data 2- Happy Ajaxing");

 

Data3.JS

 

doSomethingWithData("Data 3 - Example");

Link to comment
Share on other sites

Lai būtu saprotamāk, tomēr ieteicams izmantot kādu JS FW, piemēram, jQuery. Developēt daudz vieglāk, un nav jādomā, ka kaut kāds specifisks IE build ar tavu kodu neies. Tas, ko Uldis iepeistoja, der pašizglītības nolukos, lai saprastu, kā darbojās AJAX.

 

 

Jā, pie tā arī paliku - JQUERY

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...