+ Reply to Thread
Results 1 to 2 of 2

Thread: AJAX Website Help?

  1. #1
    Shadow121's Avatar
    Shadow121 is offline x10 Lieutenant Shadow121 is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    Centerville
    Posts
    455

    AJAX Website Help?

    I have a js file loading external files can you help me change the load time? XD

    Code:
    var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
    var loadstatustext="<img src='ajaxtabs/loading.gif' /> Requesting content..."
    
    ////NO NEED TO EDIT BELOW////////////////////////
    var loadedobjects=""
    var defaultcontentarray=new Object()
    var bustcacheparameter=""
    
    function ajaxpage(url, containerid, targetobj){
    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
    var ullist=targetobj.parentNode.parentNode.getElementsByTagName("li")
    for (var i=0; i<ullist.length; i++)
    ullist[i].className=""  //deselect all tabs
    targetobj.parentNode.className="selected"  //highlight currently clicked on tab
    if (url.indexOf("#default")!=-1){ //if simply show default content within container (verus fetch it via ajax)
    document.getElementById(containerid).innerHTML=defaultcontentarray[containerid]
    return
    }
    document.getElementById(containerid).innerHTML=loadstatustext
    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.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(revattribute){
    if (revattribute!=null && revattribute!=""){ //if "rev" attribute is defined (load external .js or .css files)
    var objectlist=revattribute.split(/\s*,\s*/) //split the files and store as array
    for (var i=0; i<objectlist.length; i++){
    var file=objectlist[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
    }
    }
    }
    }
    
    function savedefaultcontent(contentid){// save default ajax tab content
    if (typeof defaultcontentarray[contentid]=="undefined") //if default content hasn't already been saved
    defaultcontentarray[contentid]=document.getElementById(contentid).innerHTML
    }
    
    function startajaxtabs(){
    for (var i=0; i<arguments.length; i++){ //loop through passed UL ids
    var ulobj=document.getElementById(arguments[i])
    var ulist=ulobj.getElementsByTagName("li") //array containing the LI elements within UL
    for (var x=0; x<ulist.length; x++){ //loop through each LI element
    var ulistlink=ulist[x].getElementsByTagName("a")[0]
    if (ulistlink.getAttribute("rel")){
    var modifiedurl=ulistlink.getAttribute("href").replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/")
    ulistlink.setAttribute("href", modifiedurl) //replace URL's root domain with dynamic root domain, for ajax security sake
    savedefaultcontent(ulistlink.getAttribute("rel")) //save default ajax tab content
    ulistlink.onclick=function(){
    ajaxpage(this.getAttribute("href"), this.getAttribute("rel"), this)
    loadobjs(this.getAttribute("rev"))
    return false
    }
    if (ulist[x].className=="selected"){
    ajaxpage(ulistlink.getAttribute("href"), ulistlink.getAttribute("rel"), ulistlink) //auto load currenly selected tab content
    loadobjs(ulistlink.getAttribute("rev")) //auto load any accompanying .js and .css files
    }
    }
    }
    }
    }

  2. #2
    jaint is offline x10 Sophmore jaint is an unknown quantity at this point
    Join Date
    Jul 2006
    Posts
    174

    Re: AJAX Website Help?

    change the load time? That's dependent on several factors:
    - the file itself, html file will load faster since it's plaintext, while php or server side scripts will load slower since they have to be rendered on the server first. Coding of the script and intensiveness will determine the speed of rendering on the server, resource intense scripts will be slower to load.

    - network congestion, if the servers are busy -your scripts will be slower to load.

    You can't do anything about it, you just have to rely on less resource intense pages, and code optimization of the AJAX. Also if your still having problems, switch to html - ajax is new, but its not necessarily the best thing to use just because its new and popular.

    Post Message

    ----------------------------------------------------------------------------------------


+ Reply to Thread

Similar Threads

  1. Which is the best free DNS ???
    By careerbridge in forum Scripts & 3rd Party Apps
    Replies: 37
    Last Post: 09-18-2009, 12:26 PM
  2. Replies: 18
    Last Post: 09-25-2006, 04:32 PM
  3. googkle.com
    By dsfreak in forum Off Topic
    Replies: 17
    Last Post: 07-12-2006, 05:09 PM
  4. Showcase Website Awards
    By Kay in forum Graphics & Webdesign
    Replies: 2
    Last Post: 10-15-2005, 03:28 AM
  5. Using AJAX to Spy On You
    By subvertman in forum Scripts & 3rd Party Apps
    Replies: 1
    Last Post: 08-15-2005, 08:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers