forum.coppermine-gallery.net
Support => cpg1.4.x Support => Older/other versions => cpg1.4 cpmFetch by vuud => Topic started by: ericr23 on April 14, 2008, 02:56:36 pm
-
Seven months ago, I asked about how to refresh the samples on page without reloading the whole page. This weekend I learned the rudiments of AJAX, asynchronous javascript and XML, and therein found a solution. Here it is.
On the page where the gallery appears, remove the gallery code and replace it with an empty div:
<div id=galsamp></div>
Anywhere on the page, insert:
<script type="text/javascript" src=ajax-galsamp.js" />
The file ajax-galsamp.js contains the function getGalsamp():
function getGalsamp() {
var xmlHttpGalsamp=null;
try {
xmlHttpGalsamp = new XMLHttpRequest();
} catch (e) {
try {
xmlHttpGalsamp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlHttpGalsamp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
xmlHttpGalsamp.onreadystatechange = function() {
if (xmlHttpGalsamp.readyState == 4)
try {
if (xmlHttpGalsamp.status == 200) {
document.getElementById("ajax-galsamp").innerHTML
= xmlHttpGalsamp.responseText;
}
} catch (e) {
document.getElementById("ajax-galsamp").innerHTML
= "Error on Ajax return call : " + e.description;
}
}
xmlHttpGalsamp.open("get","galsamp.php");
xmlHttpGalsamp.send(null);
}
This function runs galsamp.php, which contains the gallery code, and inserts the result into the galsamp div:
<?php include_once("./pix/cpmfetch/cpmfetch.php");
$objCpm = "";
$objCpm = new cpm("./pix/cpmfetch/cpmfetch_config.php");
$objCpm->cpm_viewRandomMedia(5,1);
?>
Finally, for the ability to refresh the gallery sample, add the Refresh command to your page:
<a href="javascript:getGalsamp()">Refresh</a>
See it in action at http://www.wind-watch.org/
Note: the content of ajax-galsamp.js does not have to be in a separate file. It can be included on the page between <script type="text/javascript"> and </script>
-
Ha -- some fine example. I didn't completely update the test page in making it live. It's fixed now:
http://www.wind-watch.org/
-
Also: one error -- sorry for the carelessness.
The div id on the page needs to be ajax-galsamp, which is what the javascript function shown is looking for, not galsamp:
<div id=ajax-galsamp></div>
-
Nicely done!
I did have a chance to really play with it - but thank you very much for posting this.
I am restarting development on the next version, so any feedback on what cpmfetch could provide to make things like this work better would be welcome. I am considering working in a JSON return type.
Thanks again!
-
Thanks, Vuud!
I have one more amendment, folks. After this was working in the sidebar (an included php file), I moved it into the main body of the index page, and it no longer loaded the gallery sample until "refresh" was clicked. So I added the following to ajax-galsamp.js after the function definition to make sure it runs:
ajaxGetGalsamp();
-
Any way to make this auto-refresh every X seconds rather than include a "Refresh" link?
-
setInterval(getGalsamp, X * 1000);
-
Thanks Nibbler. I'll give it a try this evening. Will post a link if I can get this to work correctly.