Ollie Mason
Ollie Mason

Reputation: 47

Loading a div from a page using Ajax

Ok so I have a link set up:

<a href="javascript:ajaxpage('con_ucp.php?action={LIST.action_link}', 'ey_4col3');">

it loads the page into a div id, that being: #ey_4col3

The problem is it loads the entire page and its contents and all i want to load from that page is the id: #page-contents

Here is the ajax and js. I may not be using the correct method for this, in fact I'm sure I'm not. But if you can suggest a way to manipulate what I'm working with or even a cleaner simpler way to do this than I will be more than grateful:

var loadedobjects=""
var rootdomain="http://"+window.location.hostname

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)
    }

    page_request.open('GET', url, 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
        }
    }
}

Many Thanks.

Upvotes: 0

Views: 1074

Answers (1)

Anthony Grist
Anthony Grist

Reputation: 38345

The jQuery .load() function has an optional part to specify loading a page fragment. Your function would simply become:

function ajaxpage(url, containerid) {
    $('#' + containerid).load(url + ' #page-contents');
}

The $('#' + containerid) creates a jQuery object with a reference to the DOM element that has an id equal to the value of containerid. Then the .load(url + ' #page-contents') part tells jQuery to perform an AJAX call to url, then set the HTML of the #page-contents element in the returned HTML as the content of the previously selected element.

Upvotes: 3

Related Questions