0

My problem is that my code is really long and complicated, because it's normal xmlhttp and I want to switch it over to jQuery. I'm pretty sure there's shorthand functions for it? This is my current code:

function getContent() {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {

            replace_page = xmlhttp.responseText;
            doc.getElementById("content").innerHTML = replace_page;

            $("#content").children().hide();
            $("#content").children().fadeIn("slow");

            //do some other stuff
        }
        navigating = false;
    };
    xmlhttp.open("GET",site_location+"/pages/"+page+".php",true);
    xmlhttp.send();
}

I found this example, and this works perfectly, but I'm also going to have to use this for forms...

Is there a simple way I can change it? I know it'll probably involve the serialising function, right?

1 Answer 1

2

look at $.ajax();

$.ajax({
    type: 'GET',
    url: site_location+"/pages/"+page+".php",
}).done(function(html){
    $('#content').html(html).children().hide().fadeIn("slow");
})

or better using load() since the objective is to load a html content to a target element

$('#content').load(site_location+"/pages/"+page+".php", function(){
    $(this).children().hide().fadeIn("slow");
})
Sign up to request clarification or add additional context in comments.

8 Comments

Yeah, that's what I have in the example link. How would I make it work for forms tho?
Ooh! Load is better! I would have made that mistake. By doing the ajax, I could have the option of doing form content too, tho, right?
@Aarilight sorry, what do you mean by I could have the option of doing form content too, tho, right?
Like, how do I use this for submitting a form and getting the response?
@Aarilight like $.ajax({ type: 'POST', url: 'target-url', data: $('form').serialize() }).done(function (data) { //do something with data })
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.