1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Help with Javascript users online

Discussion in 'Software Development' started by PeterJS, Feb 16, 2003.

Thread Status:
Not open for further replies.
Advertisement
  1. PeterJS

    PeterJS Thread Starter

    Joined:
    Dec 14, 2002
    Messages:
    41
    Hi,

    I'm still very new to "JavaScript", but with a little help, i've produced this.........

    What its supposed to do:

    Users online script - Users can add them self to the list if they want, and view others that have added themself.

    PHP:
    <script language="JavaScript">
    var 
    username GetCookie('username');

    function 
    getCookieVal (offset) {
       var 
    endstr document.cookie.indexOf (";"offset);
       if (
    endstr == -1)
          
    endstr document.cookie.length;
       return 
    unescape(document.cookie.substring(offsetendstr));
    }
    function 
    GetCookie (name) {
            var 
    arg name "=";
            var 
    alen arg.length;
            var 
    clen document.cookie.length;
            var 
    0;
            while (
    clen) {
                    var 
    alen;
                    if (
    document.cookie.substring(ij) == arg)
                            return 
    getCookieVal (j);
                    
    document.cookie.indexOf(" "i) + 1;
                            if (
    == 0)
                                    break;
                    }
       return 
    null;
    }
    function 
    SetCookie (namevalue) {
            var 
    argv SetCookie.arguments;
            var 
    argc SetCookie.arguments.length;
            var 
    expires = (argc 2) ? argv[2] : null;
            var 
    path = (argc 3) ? argv[3] : null;
            var 
    domain = (argc 4) ? argv[4] : null;
            var 
    secure = (argc 5) ? argv[5] : false;
            
    document.cookie name "=" escape (value) +
                    ((
    expires == null) ? "" : ("; expires=" +
    expires.toGMTString())) +
                    ((
    path == null) ? "" : ("; path=" path)) +
                    ((
    domain == null) ? "" : ("; domain=" domain)) +
                    ((
    secure == true) ? "; secure" "");
    }
    document.write('<table border=0 width="92%" cellspacing="0" cellpadding="0" class="bordercolor" align="center" height="69"><tr><td height="78"><table cellpadding="4" cellspacing="1" border="0" width="100%" height="63"><tr><td class="titlebg" align="left" colspan="2"><font size="1" class="text1"><b>Additional Online Members</b></font></td></tr><tr><td class="windowbg" width="32" valign="middle" align="center" height="58"><img src="http://proboards8.com/boardimages/online.gif" border="0"></td><td class="windowbg2" valign="top" align="left" height="58" width="665"><font size="1"><table width="98%" cellpadding="3" align="center"><tr><td valign="top" align="left" width="60%"><p> '+username);
    document.write(',<font size="1"><b> </b></font></p></td></tr></table></font></td></tr></table></td></tr></table>');
    //-->
    </script>
    PHP:
    <FORM NAME="demoForm" onSubmit="if(demoForm.username.value.length != 0){   var expdate = new Date (); expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000));SetCookie('username', demoForm.username.value, expdate); return false;} else {alert('You left the Name field blank.'); return false;}">
      <
    div align="center">
        <
    table width="32%" border="0" cellspacing="0" cellpadding="0">
          <
    tr valign="top"
            <
    td align="left" width="63%"
              <
    pre>   <b>Name:</b> <input type="text" name="username" size=14></pre>
            </
    td>
            <
    td align="left" width="37%"
              <
    input type="submit" value="Add To List" name="submit" onClick="history.go(0)">
            </
    td>
          </
    tr>
        </
    table>
      </
    div>
    </
    FORM>
    I tested it out and it works fine, by saving a cookie to my PC and displaying my name everytime I view the page. I asked someone else to add there name, but it would only show there name, and not mine as well. I couldn't view there name either.

    Just wondering what's wrong, or is this not possible with JavaScript?

    Thanks

    Peter :)

    sorry if this breaks the page, lol
     
  2. brendandonhu

    brendandonhu

    Joined:
    Jul 8, 2002
    Messages:
    14,681
    A cookie is stored locally on a computer in C:\Windows\Cookies usually, not on the web server. A web page can only call a cookie to from the local system that it has set.
    Otherwise I could put a little code in my post here, and log in to anyone else's TSG account!
    JavaScript can not write to the server or hard drive (except cookies), and is useless for this kind of thing. You need a more powerful language such as PHP, CGI or ASP.
     
  3. Rockn

    Rockn

    Joined:
    Jul 29, 2001
    Messages:
    21,334
    Pssst.....Brendan, Javascript is a client side language and you can set and retreive cookies with it. It can indeed be accomplished with javascript.

    PeterS, do you have the page online with a description of how it is supposed to work and what the name of the cookie is?
     
  4. RP McMurphy

    RP McMurphy

    Joined:
    Feb 9, 2003
    Messages:
    91
    Javascript can set and retrieve cookies but since it's client side, The script runs when the client's browser parses it......they will only see themselves online. If you want a who's online feature you'll have to use a server side language like brendandonhu said.
     
  5. Rockn

    Rockn

    Joined:
    Jul 29, 2001
    Messages:
    21,334
    Or a compiled applet that runs at the server.
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/119169

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice