Uutiset:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu
Advertisement:

I need help with AJAX.

Aloittaja brianjw, toukokuu 18, 2008, 07:58:38 IP

« edellinen - seuraava »

brianjw

Hi,

I am trying to put my now playing script which currently doesn't update automatically which is why I am trying to put an auto update/refresh ajax script on the file or only part of the file. Could someone write it for me and explain with detail on how I can make this work? Thanks. :)

Darkorical

here is a simple java script to reload I found long ago and used for different things

<script type=text/javascript>
// The time out value is set to be 10,000 milli-seconds (or 10 seconds)
setTimeout(' document.location=document.location' ,1000);
</script>


brianjw

This is just an auto refresh script. I need ajax. This will stop the radio from playing when it refreshes.

I need something that doesn't reload the entire page but only updates one div or something. I don't know how to explain it...

Darkorical

I think I know what you are saying Ill try to look for a different code (basially the code used for the shout box) it reloads when there is new info and only reloads the one section


Spaceman-Spiff

Or try using this: http://www.modernmethod.com/sajax/index.phtml

The Graffiti Wall demo is pretty much an auto refresh script.

brianjw

Sorry I haven't been back to this topic.

Spaceman-Spiff, I am going to try that out now. But while you wait for my response, does anyone know how I can set up the below script to work without the start/stop buttons and just to stay on the entire time from page load to exit?
http://demos.mootools.net/Ajax_Timed

brianjw

Ok I have no idea how to get sajax working.

brianjw

Ok, I have now tried the code below:

<html>
<head>
<title>Now Playing</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
function createRequestObject() {
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}

var http = createRequestObject();

function sndReq() {
http.open('get', 'now_playing.php'); // The file that ajax is adding to the page and updating every X seconds
http.onreadystatechange = handleResponse; // This can be left alone.
http.send(null); // This can be left alone as well.
setTimeout("sndReq()", 2000); // Recursive JavaScript function calls sndReq() every 2 seconds
}

function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
if (response != responseold || responsecheck != 1) {
var responsecheck = 1;
document.getElementById("np").innerHTML = http.responseText;
var responseold = response;
}
}
}


</script>
</head>
<body onLoad="javascript:sndReq();">
<div id="np"></div>
</body>
</html>

and it shows a blank page. What else am I supposed to do? Do I need to add something to now_playing.php?

Advertisement: