Ok, theres been a few people asking for this lately so i thought id provide a script ive written. What an ajax load normaly is, is a method where an xmlhttp request is made via javascript and the content of a page is dynamically loaded in to a div of your choice.
Currently im going to provide a very basic script, which just takes a page and displays the contents in a div, via ajax.
I may later add an advanced version which will support additional function, to work with a php navigation meaning search engines can still crawl the content, the back button and favorites still works.
Basic script
Features:
> Shows page is loading
> dynamically loads page to a div.
This function can now be called via creating a link like this.Code:<script>
//Set up variables
var xmlhttp ;
var thediv;
function LoadPage(page,usediv) {
// Set up request varible
try {xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");} catch (e) { alert("Error: Could not load page.");}
//Show page is loading
document.getElementById(usediv).innerHTML = 'Loading Page...';
//scroll to top
scroll(0,0);
//Store div in use
thediv = usediv;
//send data
xmlhttp.onreadystatechange = ShowPage;
xmlhttp.open("GET", page);
xmlhttp.send(null);
//Stop any link loading normaly
return false;
}
function ShowPage(){
//Check page is completed and there were no problems.
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
//Write data returned to page
document.getElementById(thediv).innerHTML = xmlhttp.responseText;
}
}
</script>
<a href="mypage.htm" onclick="return LoadPage('mypage.htm','mydiv');">
The above would place the contents of "mypage.htm" in to a div with the id "mydiv" such as could be written '<div id="mydiv"></div>'
The parts of the link in bold being what need editing.
I wrote the script on the go so its completely untested but should hopefully work. If it doesnt post back with the problem and ill sort it out for you.
Edit: phew noticed just in time, id forgotten to store the div id in the function independents variable, which would have stopped it from working, should be fixed now.

