How do I update a div using ajax?

by on May 5, 2010

Q: In the <head> of my HTML file, I have included 3 JavaScript files:

<script type=”text/javascript” src=”/cufon/cufon-yui.js”></script>
<script type=”text/javascript” src=”/cufon/cufon-config.js”></script>
<script type=”text/javascript” src=”/cufon/Effloresce_400-Effloresce_700.font.js”></script>

I’m working with ajax: not a complete page refresh, only a refresh of a <div> content.

When the refresh happens, I need the js files to change my font! The js should be in here too:

this.menu = function() {
var divID = “scroll”;
if (typeof(arguments[0])!=’undefined’)
divID = arguments[0];

var pAjax = new Ajax();
pHTTPRequest = pAjax.Init();
pHTTPRequest.onreadystatechange=function() {
if (pHTTPRequest.readyState==1) {
//document.getElementById(divID).innerHTML = “Aan het laden”;
}
if (pHTTPRequest.readyState==4) {
document.getElementById(divID).innerHTML = pHTTPRequest.responseText;
}
}

var strParams = “type=menu&menu_id=”+arguments[1]+”&link=”+arguments[2];
pHTTPRequest.open(‘POST’, ‘/include/ajax.php’, true);
pHTTPRequest.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded’);
pHTTPRequest.setRequestHeader(‘Connection’, ‘close’);
pHTTPRequest.send(strParams);

}

But how?!


One Response to “How do I update a div using ajax?”
  1.  

    First I would suggest switching over to using jquery as it is much easier library to use and get help on.

    Second you will actually need to change the class associated with your div if you want to change the font being used unless of course you use font tags within a div but that would be rather weird.

    Can you maybe give a little bit more explanation of exactly what you are doing and why you are trying to do it? If it is to show an error then something like jQuery Mini UI is nice where you actually wiggle the error message.

    In their example they wiggle the boxes but a lot of times developers will have their error text set to red and use a library like that to wiggle the error message so people notice it.