FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Shoutbox Problem

 


DanielXP
Hello.

My shoutbox I have made seems to say "undefined" at the start of it.

This is my javascript code.

Code:
function getshouts() {
var shouts_box = document.getElementById('shouts');
var shoutbox_text;
jsongetshouts=GetXmlHttpObject()
   if(jsongetshouts) {
      jsongetshouts.open("POST", "<?=$siteurl?>ajax/shoutbox.php");
      jsongetshouts.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      jsongetshouts.onreadystatechange = function () {
         if (jsongetshouts.readyState == 4) {
            if (jsongetshouts.status == 200 || jsongetshouts.status == 0) {
               var response = eval("(" + jsongetshouts.responseText + ")");
               for(i=0;i < response.shoutbox.message.length; i++) {
               if (i%2) { rocolor = "2"; } else { rocolor = ""; }
                  shoutbox_text += "<div class=\"conbgnb"+rocolor+"\">";
                  shoutbox_text += "<strong><a href=\"<?=$siteurl?>profile/"+response.shoutbox.message[i].poster+"/\" class=\"tutoriallink\">"+response.shoutbox.message[i].poster+":</a></strong><br />";
                  shoutbox_text += response.shoutbox.message[i].msgtext;
                  shoutbox_text += "</div>";
               }
            shouts_box.innerHTML = shoutbox_text;
            }
         }
      };
      jsongetshouts.send("key=<?=$ajaxkey?>");   
   }
}


And this is the JSON code,

Code:
{"shoutbox":
   {"message":[
      {"msgid":  "2",
         "poster": "You",
         "msgtext": "hello me"
      },{"msgid":  "1",
         "poster": "Me",
         "msgtext": "hello you"
      } ]
   }
}


The code it puts into the the div (shouts_box.innerHTML)
Quote:
undefined<div class="conbgnb"><strong><a href="profile/DanielXP/" class="tutoriallink">DanielXP:</a></strong><br />same again Razz</div><div class="conbgnb2"><strong><a href="profile/DanielXP/" class="tutoriallink">DanielXP:</a></strong><br />testing</div>


Thanks 4 ya help Smile
DanielXP
Any help yet?
Peterssidan
What happens is that that you first declare the variable shoutbox_text:
Code:
var shoutbox_text;
but it's still undefined.

later you come to this code:
Code:
shoutbox_text += "<div class=\"conbgnb"+rocolor+"\">";
which is the same as
Code:
shoutbox_text = shoutbox_text + "<div class=\"conbgnb"+rocolor+"\">";
shoutbox_text is converted to a string, which is "undefined" and concatenated to the rest of the string.

To solve this just change the declaration to:
Code:
var shoutbox_text = "";
DanielXP
Thanks.

Also i have this code

Code:
function getstuff() {
clearTimeout(stufftimer);
var shouts_box = document.getElementById('shouts');
var shoutbox_text;
xmlgetstuff=GetXmlHttpObject()
   if(xmlgetstuff) {
      xmlgetstuff.open("GET", "<?=$siteurl?>ajax/stuff.php", true);
      xmlgetstuff.onreadystatechange = function () {
         if (xmlgetstuff.readyState == 4) {
            if (xmlgetstuff.status == 200 || xmlgetstuff.status == 0) {
               var xmlDocument = xmlgetstuff.responseXML;
               glbmsg = xmlDocument.getElementsByTagName("glbmsg");
               if(glbmsg.length > 0) {
                  var ntext = glbmsg[0].firstChild.data.replace(/\n/g, "<br />");
                  Ext.MessageBox.alert('Message', ntext);
               }
               stufftimer = setTimeout('getstuff()', '25000')
            }
         }
      };
      xmlgetstuff.send(null);   
   }
}


It post 1 shout ok,

But if you try and post anoher one

Quote:
postshout is not a function
[Break on this error] postshout();


And you get that until you refresh and then again can only post one shout.
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.