Page 1 of 1

FIXED: AJAX includes to allow the passing of HTTP queries.

Posted: Sat Dec 29, 2007 8:26 am
by JAB Creations
I've been getting better at JavaScript and PHP of late though this one is still a bit beyond me. Currently this works great as an AJAX includes (if you know what AJAX does and what an includes in PHP does then you just add 1+1) though there is an issue: this script can not pass HTTP queries such as file.php?query_property=query_value.

Working Function Example

Code: Select all

ajaxpage('includess/ajax-example.php', 'promptsajax');
NOT working Function Example

Code: Select all

ajaxpage('includess/ajax-example.php?query_property=query_value', 'promptsajax');
AJAX Includes JavaScript

Code: Select all

var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
} 
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
//page_request.open('GET', url+bustcacheparameter, true)
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}

Posted: Sat Dec 29, 2007 2:45 pm
by Kieran Huggins
Dude - you really need to ditch all this crap and embrace jQuery. Wheel re-invention is totally unnecessary in this case.

Posted: Sat Dec 29, 2007 7:15 pm
by JAB Creations
Every single file compressed (images, JavaScript, CSS, PHP, etc) jQuery doubles the bandwidth required for the page to load.

If someone actually helps me with this I'll show how I take the single contact form and with minimal effort make it work with an unlimited number of people.

Posted: Sat Dec 29, 2007 7:23 pm
by Kieran Huggins
externals are cached, so is a one time hit of 14k per user really worth worrying about?

Posted: Sat Dec 29, 2007 7:36 pm
by JAB Creations
My news page goes from 37KB to 61KB. That's from 10 seconds to 16 seconds of load time on a 36K modem (56K speeds don't exist due to dial-up limitations).

What is the function you're dead set on me using? Maybe I can strip it's code out of jQuery and use it directly instead.

Posted: Sat Dec 29, 2007 8:01 pm
by Kieran Huggins
load()

You use it like this:

Code: Select all

$('#load_stuff_here').load('url_to_load_from.php',{key1:val1,key2:val2});
You can also specify a callback function, if you like.

Get all the details here:
http://visualjquery.com/1.1.2.html under Ajax > Load

Posted: Sat Dec 29, 2007 11:03 pm
by JAB Creations
So I'll have to dig through jQuery for a load() function it looks like. Maybe I'll be able to get the code to work standalone.

Posted: Tue Jan 01, 2008 12:38 am
by JAB Creations
Issue Resolved: I had the same function declared twice. :roll: