bennyboywonder Posted February 6, 2007 Share Posted February 6, 2007 Currently I am experementing with ajaxing my blog. I am learning to use the XMLHttpRequest object, but am finding it a little sporadic. It appears to work and not work sporadically. For example the below function works is fired up as soon as the page loads, writing all the entries onto the main DIV. However, if I create a new function by litterally copying and pasting, and then have this function fired by a form button, the code in the onreadystatechange function, doesn't seem to fire anymore. Can anyone else help me with this. It seems rather arbitrary. function loadPosts(month, year, entry) { toggleLayer('mainloadingbox'); var querystring = "action=readblog"; if(entry) querystring += "&entry=" + entry; else if (month && year) querystring += "&month=" + month + "&year=" + year; var url = "scripts/action.php?" + querystring; var xhReq = createXMLHttpRequest(); xhReq.open("GET", url, true); xhReq.onreadystatechange = function() { if (xhReq.readyState != 4) { return; } if (xhReq.status != 200) { return; } var xml = xhReq.responseXML.getElementsByTagName("post"); toggleLayer('mainloadingbox'); for (var i = 0; i < xml.length; i++) { titleNode = xml[i].getElementsByTagName("title"); posttitle = "<h1>" + titleNode[0].firstChild.data + "</h1>"; bodyNode = xml[i].getElementsByTagName("body"); body = "<div>" + bodyNode[0].firstChild.data + "</div>"; with(document.getElementById("main")) { innerHTML += posttitle; innerHTML += body; } } } xhReq.send(null); } Quote Link to comment Share on other sites More sharing options...
tomfmason Posted February 6, 2007 Share Posted February 6, 2007 Well... First off I normally create a separate function for dealing with the xmlhttprequest. This will make it easier and reusable. Here is a simple cross browser example of what I mean. function createRequestObject() { if (window.XMLHttpRequest) { // Mozilla, Safari, Opera... var xmlhttp = new XMLHttpRequest(); if (xmlhttp.overrideMimeType) xmlhttp.overrideMimeType('text/xml'); } else if (window.ActiveXObject) { // IE try { var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!xmlhttp) { alert('Giving up Cannot create an XMLHTTP instance'); return false; } return xmlhttp; } var XhReq = createRequestObject(); If you are not dealing with xml then you will want to comment these two lines or you will get an error in FF if (xmlhttp.overrideMimeType) xmlhttp.overrideMimeType('text/xml'); i think that if you do it like this you will find that it is more stable. Tom Quote Link to comment Share on other sites More sharing options...
bennyboywonder Posted February 6, 2007 Author Share Posted February 6, 2007 Well... First off I normally create a separate function for dealing with the xmlhttprequest. This will make it easier and reusable. Here is a simple cross browser example of what I mean. function createRequestObject() { if (window.XMLHttpRequest) { // Mozilla, Safari, Opera... var xmlhttp = new XMLHttpRequest(); if (xmlhttp.overrideMimeType) xmlhttp.overrideMimeType('text/xml'); } else if (window.ActiveXObject) { // IE try { var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!xmlhttp) { alert('Giving up Cannot create an XMLHTTP instance'); return false; } return xmlhttp; } var XhReq = createRequestObject(); If you are not dealing with xml then you will want to comment these two lines or you will get an error in FF if (xmlhttp.overrideMimeType) xmlhttp.overrideMimeType('text/xml'); i think that if you do it like this you will find that it is more stable. Tom Thanks, but I already had one, that's what createXMLHttpRequest() was. That isn't the problem... function createXMLHttpRequest() { try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {} try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} try { return new XMLHttpRequest(); } catch(e) {} alert("XMLHttpRequest not supported"); return null; } Quote Link to comment Share on other sites More sharing options...
bennyboywonder Posted February 7, 2007 Author Share Posted February 7, 2007 ok, well I've sorted this now, so never mind fenway: how? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.