String into html with Javascript

Discussion in 'JavaScript' started by fex, Jul 2, 2008.

  1. #1
    Hi,

    I wrote a Javascript to see who's logged in on my website. In the script I simply create a string of the names of the people who are online.

    I was wondering how I could get that string into <p></p> tags on my webpage.

    I'm not sure about the return statement to retreive the string, maybe I need to use print or echo in php (with the js inside the echo).. That's why I'm asking here :)
    <script type="text/javascript">
    	function onlineMembers(){
    		
    		var usersOnline = new Array(7);
    		var users = new Array(7);
    		online = "";
    		
    		usersOnline[0] = "laurens";
    		usersOnline[1] = "maarten";
    		usersOnline[2] = "jana";
    		usersOnline[3] = "leen";
    		usersOnline[4] = "pipi";
    		usersOnline[5] = "salm";
    		usersOnline[6] = "gerry";
    		
    		users [0] = "Laurens";
    		users [1] = "Maarten";
    		users [2] = "Jana";
    		users [3] = "Leen";
    		users [4] = "Pipi";
    		users [5] = "Salm";
    		users [6] = "Gerry";
    		
    		if(document.cookie.length > 0){   // if there are any cookies
    			for(i=0; i<7; i++){
    				search = usersOnline[i] + "=";
    				offset = document.cookie.indexOf(search);
    				if(offset != -1){   // if cookie exists
    					online = online + "» " + users[i] + "\n\n";   // Second array for capitals
    				}
    			}
    		}
    		else{
    			online = "Geen leden online";   // When no members online
    		}
    		return online;
    	}
    </script>
    Code (markup):
     
    fex, Jul 2, 2008 IP
  2. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #2
    assign a ID to the P tag, so you can call and fill it using document.getElementById("idname").innerHTML = "put the new content of the p here";
     
    crath, Jul 2, 2008 IP
  3. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thx for your fast response, was on vacation tho, so wasn't able to answer :)

    I've assigned an ID to the p tag which works. But is the coding with the cookies alright ? Cause I'm not getting what I should,,

    		if(document.cookie.length > 0){   // if there are any cookies
    			for(i=0; i<7; i++){
    				search = users[i] + "=";
    				offset = document.cookie.indexOf(search);
    				if(offset != -1){   // if cookie exists
    					online += "» " + users[i] + "\n\n"; 
    				}
    			}
    		}
    		else{
    			online = "Geen leden online";   // When no members online
    		}
    		//return online;
    		online += "bla";
    		document.getElementById('members_online').innerHTML = online;
    Code (markup):
    The only thing I'm seeing on my webpage (logged in or not) is "bla"..
     
    fex, Jul 25, 2008 IP
  4. =Messa=

    =Messa= Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I tested your code and it works OK.

    Can you post the cookie(s) string you're testing this with?

    Keep in mind, that the indexOf() method you're using is case sensitive.
     
    =Messa=, Jul 26, 2008 IP
  5. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Mmmh, your right, the code does work. You put me to thinking, thankfully I noticed I set my cookies as this:

    setcookie("loginname", $loginname, time()+3600);

    So I needed to see if it's the value of the cookie (if it exists) that matches one of the people in my users array.
    I had to modify the code a bit, the result:

    <script type="text/javascript">		
    		var users = new Array(7);
    		var member = "";
    		var online = "";
    		
    		users[0] = "laurens";
    		users[1] = "maarten";
    		users[2] = "jana";
    		users[3] = "leen";
    		users[4] = "pipi";
    		users[5] = "salm";
    		users[6] = "gerry";
    
    		if (document.cookie.length>0){
    			c_name = 'loginname';
    			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;
    		    	}
    		    member = unescape(document.cookie.substring(c_start,c_end));
    		    } 
    		    for(i=0; i<7; i++){
    		    	if(member == users[i]){
    		    		online += "» " + users[i] + "<br/>";
    		    	}
    		    }
    		}
    
    		document.getElementById('members_online').innerHTML = online;
    	</script>
    Code (markup):
    I guess this should work since a cookie is stored on your computer and you can only have one cookie named 'loginname' at the same time.. If not, please correct me ;)

    EDIT: It cannot work, I need to 'write all online members down' on one page and include that one in my webpage.
    I would do it like this:

    $fp = fopen("data.php", "a");
    fputs($fp, "<p>$online_members</p>");
    fclose($fp);

    but my cookies expire after an hour,,
     
    fex, Jul 28, 2008 IP
  6. jing3142

    jing3142 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    could be a problem in your setcookie() function or in your time() function. Need to see the code for these.

    This site gives some cookie functions that work for me.

    http [colon] //www [dot] quirksmode [dot] org/js/cookies [dot] html

    as a newbie cannot give a live link yet
     
    jing3142, Jul 29, 2008 IP
  7. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    fex, Jul 29, 2008 IP