10 minutes
Wednesday, February 27th, 2008
this is going to be a ten minute post- hopefully it will be a meaningful 10 minutes.
up until last night i had a problem. i’m working on a project. it’s a website that plays video. the page is simple- it only has the title banner across the top, the main div where the video goes, and a few navigation links under that. what i needed was a way to switch between videos without reloading the page, just the div. flash is out of the question- i work only with xhtml, css, javascript, and php.
after some googling, i came across this. for some reason, i knew as soon as i saw this that it was exactly what i needed. after a little more inspection, these two lines confirmed it:
xmlhttp.open(”GET”,url,true);
and
onload=”loadXMLDoc(’test_xmlhttp.txt’)”
i noticed that the source for the text swapped into the div was a text file. up until this example, all the “div content swap” solutions i found used text that was in a preloaded div with display:none; to keep it hidden until whatever action called it. it didn’t seem to be a far stretch to change that path to that text file to the path to the videos i was using.
the fact that the second argument passed to the xmlhttp.open() function is actually called “url” confirmed it. i just needed to provide it with the url i wanted.
this is where the functions that i had previously written came in handy. instead of calling the loadXMLDoc function right away, clicking on the previous or next function calls would generate the correct path. instead of loading the contents of “test_xmlhttp.txt”, the
function would load the correct path. ie “http://…1.html” or whatever. it was just a matter of changing the last line of the script to pass the correct url to the function.
here’s the rest of my javascript.
var videoNumber = 1;
// initialize first video
// to http://…whatever…/1.htmlvar path = “http://…whatever…/videos/”;
var extension = “.html”;function nextVideo(){
if (videoNumber < 3)
updateNumber(videoNumber += 1);// increment current video's number,
// pass value to updateVideo()else updateNumber(videoNumber = 1);
}function previousVideo(){
if (videoNumber > 1)
updateNumber(videoNumber -= 1);// decrement current video’s number,
// pass value to updateVideo()else updateNumber(videoNumber = 3);
}function updateNumber(currentVideoNumber){
var newVideoAddress = path + currentVideoNumber + extension;// create new video’s address
loadXMLDoc(newVideoAddress);// pass address to reloading function
}
weird how those two scripts seem like they were written to work together. comments and critiques are always welcome.
ps- i removed the actual path for now. it wasn’t the final one anyway. i’ll be sure to mention once this is good and ready.
pps- this took me 17 minutes to write.