First off, thanks to this community. I'm slowly starting to understand this stuff and I appreciate all the feedback. That said, I come with another question.
THE SITUATION: I'm trying to build a new site and I'm trying to use it as a learning experience to use some php and teach myself javascript (note, I'm wanting to stick with js right now and not jump to jquery until I get a better handle on js). I've created a gallery that will have a series of thumbnails at the top that when clicked will populate a div below with artwork, its title and a description.
THE SETUP: Because I want to have these called by names and not numbers (such as an array) so they can be interchangeable, I built each as an id's div containing all the components and then I'm calling it to the target location via the script:
function changeDiv(id) {
var target = document.getElementById('generic');
var id = document.getElementById(id);
target.innerHTML = id.innerHTML;
}
In the body it looks like:
<img src="images/thumbs/digital-art/thumb-space-oddity.jpg" id="thumb-space-odity" onclick="callDiv('space-oddity')"/>
...
<div class="gallery-frame" id="generic"></div>
Currently this works but since I have to have a place for all of the divs to be called from I've added a php include in the footer so it loads last but contains all of the images I could possibly need:
<div style="display:none;">
<?php include("gallery.php"); ?>
</div>
... but from what I understand since this will still mean the page will load all the content that will really slow down the page. So I need a new solution.
MY CHALLENGE: First off, is there a way to do this exclusively in javascript? If so I'd gladly do that!
Assuming I cannot, I have started looking into AJAX to see how it works and while it's starting to make nominal sense (still new to javascript so I only get half of it) and between w3schools.com and the stackoverflow topic I think i have a general idea of what I need to do but it's still not working.
THE PROBLEM: Based on the above stackoverflow comment I attempted to recreate the example function in my own instance and I have set it up as the following:
<script type="text/javascript">
function createXMLHttpRequest() {
try {
return new XMLHttpRequest();
}
catch (e)
{ alert('XMLHttpRequest not working'); }
try {
return new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{ alert('Msxml2.XMLHTT not working'); }
try {
return new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{ alert('Microsoft.XMLHTTP not working'); }
alert("XMLHttpRequest not supported");
return null;
}
function callDiv(id) {
var xmlHttpReq = createXMLHttpRequest();
var url = "gallery.asp?ID=" + id;
xmlHttpReq.open("GET", url, true);
xmlHttpReq.send();
}
</script>
From what I've read I left the first section alone as that sets up the function (removing the null and prompts as I don't need them) and trying to have the callDiv function run the same as the previous script I was using. I have also saved out the gallery.php page as an .asp page but should I have left it as php?
Did it have to be .ashx?
And since I don't know the file it was being called from I don't know what the '?REASON' that was used in the original example meant. I tried ?ID thinking I was telling it to look for an id. Is that wrong, or unnecessary?
Thanks!
-Chris