Ooops, sorry I'm used to quoting things. :(
Printable View
=D =D =D =D
Mine has less crap in it than DD's.
DD's stuff always seems to be incredibly long winded and inefficient for no real reason "/
On the topic of including to much crap i combined it in to a single function, i think it will still work, i haven't tested again, main differences is i put the second function in the first.
Im working on the assumption formating like this means i dont need to define a globally scoped variable (which cases problems if your actually useing the asynchronous part of ajax), and that i used the inline function syntax right.Code: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);
//send data
xmlhttp.onreadystatechange = function(){
//Check page is completed and there were no problems.
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
//Write data returned to page
document.getElementById(usediv).innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", page);
xmlhttp.send(null);
//Stop any link loading normaly
return false;
}
anway to make the content load so many pixels to the rite of the beginning of the content box? and thanks mentor
do i still have news privladges on your site?
not sure why you would want to, but if your asking what i think you are, easyst way would just be add a margin or padding in CSS to the div your loading the content in to ?
Also just for the hell of it, even smaller version
Code:function LoadPage(p,d) {
try {xh = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");} catch (e) { alert("Error: Could not load page.");}
pd = document.getElementById(d); pd.innerHTML = 'Loading Page...'; scroll(0,0);
xh.onreadystatechange = function(){if ((xh.readyState == 4) && (xh.status == 200)) {pd.innerHTML = xh.responseText;}}
xh.open("GET", p); xh.send(null); return false;
}
Good work bag:D
As an exspantion, an updated version which now allows people to bookmark pages loaded via ajax. And Allows the backbutton to function correctly in firefox (not IE as that doesnt change the URI when a new hash is set)
Long version
Short versionCode:var CurrentPage;
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);
//Change loaction hash.
document.location.hash = page;
//Store current page.
CurrentPage = document.location.hash;
//send data
xmlhttp.onreadystatechange = function(){
//Check page is completed and there were no problems.
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
//Write data returned to page
document.getElementById(usediv).innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", page);
xmlhttp.send(null);
//Stop any link loading normaly
return false;
}
//Poll hash to see if back button has been pressed
setInterval(CheckBack, 500);
function CheckBack(){
// if the hash has changed from what was set in the script reload page.
if(CurrentPage !== document.location.hash){
LoadPage(document.location.hash);
}
}
Again both are completely untested but should work just fineCode:var cp;
function LoadPage(p,d){
try {xh=window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");} catch (e){alert("Error: Could not load page.");}
pd=document.getElementById(d);h=document.location.hash;pd.innerHTML='Loading Page...';scroll(0,0);h=p;cp=h;
xh.onreadystatechange=function(){if((xh.readyState==4)&&(xh.status==200)){pd.innerHTML=xh.responseText;}}
xh.open("GET",p);xh.send(null);return false; }
function CheckBack(){if(cp!==document.location.hash){LoadPage(document.location.hash);}} setInterval(CheckBack,500);