PICS Posted December 31, 2008 Share Posted December 31, 2008 Hey guys I'm creating a page that utilizes a cookie to set the town. Now my cookie is set fine. The problem occurs when I pull down the cookie and try to pull the correct page with AJAX. The code is below: Cookie Retrieval function getCookie(c_name) { if (document.cookie.length>0) { c_start=document.cookie.indexOf(c_name + "="); if (c_start!=-1) { c_start=c_start + c_name.length+1; c_end=document.cookie.indexOf(";",c_start); if (c_end==-1) c_end=document.cookie.length; return unescape(document.cookie.substring(c_start,c_end)); } } return ""; } function checkCookie() { serverPage= getCookie('cp_town'); serverPage = "/section/fptest?kword=" + serverPage; if (serverPage!=null && serverPage!="") { var obj = document.getElementById('townbox'); xmlhttp.open("GET", serverPage); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { obj.innerHTML = xmlhttp.responseText; } } xmlhttp.send(null); oldserverpage = serverPage; serverPage = serverPage.replace("/section/fptest?kword=", ""); document.getElementById('mytown').innerHTML = "<h3>" + serverPage.replace(/_/i, " ") + "</h3>"; } } checkCookie(); Any idea why it keeps giving me an error about the document.getElementById('mytown') portion? It tells me it's null. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted December 31, 2008 Share Posted December 31, 2008 The document DOM probably hasn't loaded at the time this is run. I assume this is at the top of your page? 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.