-
June 2nd, 2010, 10:55 AM
#1
Questions about my first AJAX script.
hey guys!
I finally have a working ajax script, it's from a tutorial and i have altered it to my liking.I have some questions regarding it. Here's the code.
Code:
<script type="text/javascript">
//scanner AJAX
function Ajax()
{
var scanner;
try
{
scanner=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari
}
catch (e)
{
try
{
scanner=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e)
{
try
{
scanner=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser has no ajax support!"); return false;
}
}
}
scanner.onreadystatechange=function()
{
if(scanner.readyState==4)
{
document.getElementById('scanner').innerHTML=scanner.responseText;
setTimeout('Ajax()',2000);
}
}
scanner.open("GET","navscanner.php",true);
scanner.send(null);
}
window.onload=function()
{
setTimeout('Ajax()',2000);
}
</script>
So this outputs a table inside a div in my page that i generate with navscanner.php
Now i have a lot more div's to be filled like this. And i'm kinda stuck to where to put the new code.
1) Do i need to make the browser check each time i do a ajax request?
2)If i need more divs to refresh each 2 second like above code can i use the above code to make that happen or do i need a totally new function?
3)same question as 3 but for div's that need to get loaded on start and when a player clicks something. Can i put all this code inside the Ajax function or do i need sepperate functions for each?
Last edited by menyo; June 2nd, 2010 at 11:07 AM.
-
June 5th, 2010, 02:52 AM
#2
Re: Questions about my first AJAX script.
There are 2 ways you can do this. If you want all the divs to refresh at once, the simplest way is to hit the server, and work with the responseText (Or responseXML).
XML is nice when you are working with several items, but responseText is by far the easiest way.
You could simply return (from the ajax request
Code:
Content for Div #1|Content for Div #2|Content for Div #3
And use:
Code:
split = scanner.responseText.split("|");
document.getElementById('scanner').innerHTML=split[0];
document.getElementById('scanner2').innerHTML=split[1];
document.getElementById('scanner3').innerHTML=split[2];
You could also just do multiple ajax requests for each div (Not recommended, unless you need them at different times)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|