www.webdeveloper.com
Results 1 to 3 of 3

Thread: What am I doing wrong?

  1. #1
    Join Date
    Dec 2010
    Posts
    19

    What am I doing wrong?

    The script i'm trying to write consist of asking the user for his name in a form format which is then sent in a cookie. When the user reconnect to the same page, it displays : Hi, "HISNAME", welcome to my site! I don't know what I'm doing wrong, seriously.



    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled Document</title>
    <script type="text/javascript">
    if ( ! get_cookie ( "username" ) )
    {
    var username = document.write(

    <form name="name">

    <table>

    <tr>

    <td valign="top">

    Name :

    </td>

    <td>

    <input type="text" name="texte" value="">

    </td>

    <td>

    <INPUT TYPE="button" NAME="bouton" Value="Click" onClick="testResults(this.form)">

    </td>

    </tr>

    </table>

    </form>);

    if ( username )
    {
    var current_date = new Date;
    var cookie_year = current_date.getFullYear ( ) + 1;
    var cookie_month = current_date.getMonth ( );
    var cookie_day = current_date.getDate ( );
    set_cookie ( "username", username, cookie_year, cookie_month, cookie_day );
    }
    }
    else
    {
    var username = get_cookie ( "username" );
    document.write ( "Hi " + username + ", welcome to my website!" );
    document.write ( "<br><a href=\"javascript:delete_cookie('username');
    document.location.reload ( );\">
    Forget about me!</a>" );
    }
    </script>
    </head>
    <body>
    </body>
    </html>

  2. #2
    Join Date
    Aug 2009
    Posts
    593
    Where are these get_cookie and set_cookie functions defined, also the way you are writing out the <form> using document.write looks incorrect?

    http://www.w3schools.com/JS/js_cookies.asp
    Last edited by thraddash; 01-26-2011 at 03:48 PM.

  3. #3
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Everything in document.write needs to be in between the ( and ) on one line, and needs to be surrounded by " and ", which things like name="name" would break.

    You could set a var thisContent = ""; then use the shortcut for appending the data and split it up across several lines, then document.write(thisContent);

    Code:
    var thisContent = "";
    thisContent += "blah blah blah blah";
    thisContent += "bleh bleh bleh bleh";
    // and escape the "
    thisContent += "<input name=\"name\" />";
    
    document.write(thisContent);
    ^_^

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles