Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple script doesn't work

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    
    <script type="text/javascript" language="javascript">
    var loggedin = ~*loggedin*
    if (loggedin) {
    document.getElementById('login').innerHTML = '<p>Logged in as: ~*loggedin_name*</p>';
    } else {
    document.getElementById('login').innerHTML = '
    <table width="422" border="1" cellspacing="0" cellpadding="0"><form ~*login_form_properties*>
      <tr>
        <td colspan="2">Login:</td>
      </tr>
      <tr>
    	<td width="250">Username:</td>
        <td width="166"><input ~*login_usernameinput_properties*></td>
      </tr>
      <tr>
    	<td>Password:</td>
        <td><input ~*login_passwordinput_properties*></td>
      </tr>
      <tr>
    	<td>Remember:</td>
        <td><input ~*login_rememberinput_properties*></td>
      </tr>
      <tr>
        <td colspan="2"><input ~*login_registerbutton_properties*> <input ~*login_loginbutton_properties*></td>
      </tr>
    </form></table>
    ';
    }
    </script>
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <table width="250" border="1" cellspacing="0" cellpadding="0">
      <tr>
        <td id="login"></td>
      </tr>
      <tr>
        <td id="body">~*body*</td>
      </tr>
      <tr>
        <td>Hoi Hoi Hoi </td>
      </tr>
    </table>
    </body>
    </html>
    It just leaves the <td id="login"> empty... I tried putting the script in the <body> tag but no change...

    Edit: It does work when you leave out the "else {}" part
    Last edited by Ge64; 12-18-2006 at 07:18 AM.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You should be getting a js error because the td you are trying to write to likely doesn't exist yet. You need to use window.onload.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <script type="text/javascript">
    var loggedin = 1
    window.onload = function()
    {
    	if (loggedin)
    	{
    		document.getElementById('login').innerHTML = '<p>Logged in as: Ge64</p>';
    	}
    }
    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    </head>
    <body>
    <table width="250" border="1" cellspacing="0" cellpadding="0">
    	<tr>
    		<td id="login"></td>
    	</tr>
    	<tr>
    		<td id="body">~*body*</td>
    	</tr>
    	<tr>
    		<td>Hoi Hoi Hoi </td>
    	</tr>
    </table>
    </body>
    </html>
    Another way you could do what you want using DOM is by doing something like this
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <script type="text/javascript">
    var loggedin = 1
    window.onload = function()
    {
    	if (loggedin)
    	{
    		document.getElementById('login').firstChild.nodeValue = 'Logged in as: Ge64';
    	}
    }
    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    </head>
    <body>
    <table width="250" border="1" cellspacing="0" cellpadding="0">
    	<tr>
    		<td><p id="login">&nbsp;</p></td>
    	</tr>
    	<tr>
    		<td id="body">~*body*</td>
    	</tr>
    	<tr>
    		<td>Hoi Hoi Hoi </td>
    	</tr>
    </table>
    </body>
    </html>
    Though checking to see if a user is logged in or not really needs to be done using a server side script.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New Coder
    Join Date
    Dec 2006
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, sorry I edited the start post and found out where the problem is. I also tried putting the entire script just before the </body> tag (after the <td>), and still nothing happened.

    I replaced all "s in the string with \"s, but no difference. I replaced the '....' with "....", but no change. Is it because the string covers multiple lines?

    The login checking is done by a PHP script which places the value (1 or 0) of the var loggedin before sending it to the client.

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Okay then I'm a little confused why not do something like
    PHP Code:
    <?php
    $message 
    '';
    $loggedin 1// set by php stuff
    if(loggedin)
    {
        
    $message 'Logged in as: '.$username;
    }
    else
    {
        
    $message 'Welcome, guest!';
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    </head>
    <body>
    <table width="250" border="1" cellspacing="0" cellpadding="0">
        <tr>
            <td id="login"><p><?php echo $message?></p></td>
        </tr>
        <tr>
            <td id="body">~*body*</td>
        </tr>
        <tr>
            <td>Hoi Hoi Hoi </td>
        </tr>
    </table>
    </body>
    </html>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    New Coder
    Join Date
    Dec 2006
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm making a system that works with templates, so template makers should provide all HTML code for both the login form (username: input, password: input etc) and the same for when somebody is already logged in, in one HTML file. unless I can make a hidden <td> and let the javascript chose which of the 2 is displayed, I need to do something like this.

    I want the php script to only provide the data itself (e.g. the username of the user currently logged in), the rest should be part of the template file...

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Okay I guess but if you read my first post you would have the solution to your problem. The element DOES NOT exist therefore you can't write to it until AFTER page load. Your innerHTML string in the else statement is malformed. It needs to be all on one line or you need to do something like this
    Code:
    .innerHTML = '<table width="422" border="1" cellspacing="0" cellpadding="0"><form ~*login_form_properties*>'
      +'<tr>'
        +'<td colspan="2">Login:</td>'
      +'</tr>'
      +'<tr>'
    	+'<td width="250">Username:</td>'
    // do same for rest of html
    ';
    Last edited by _Aerospace_Eng_; 12-18-2006 at 08:27 AM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #7
    New Coder
    Join Date
    Dec 2006
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    Okay I guess but if you read my first post you would have the solution to your problem. The element DOES NOT exist therefore you can't write to it until AFTER page load.
    I did read it, I also tried it. I think you missed my edit of the start post, the code I posted originally turned out to work but if I add the login form part it doesn't work anymore. I also put the entire script after the table so the TD would exist first, but it didn't make a difference.

  • #8
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    I made an edit to my last post reread it. You'll find out why it doesn't work. Again you should be receiving javascript errors something like unterminated string constant. What browser are you using?
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #9
    New Coder
    Join Date
    Dec 2006
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    I made an edit to my last post reread it. You'll find out why it doesn't work. Again you should be receiving javascript errors something like unterminated string constant. What browser are you using?
    Ok thx, i'm using FFv2 and I dont get any errors...

    Edit:

    I put everything on 1 line like this:

    Code:
    <script type="text/javascript" language="javascript">
    var loggedin = ~*loggedin*
    window.onload = function()
    {
    if (loggedin) {
    document.getElementById('login').innerHTML = '<p>Logged in as: ~*loggedin_name*</p>';
    } else {
    document.getElementById('login').innerHTML = "<table width=\"422\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\"><form ~*login_form_properties*><tr><td colspan=\"2\">Login:</td></tr><tr><td width=\"250\">Username:</td><td width=\"166\"><input ~*login_usernameinput_properties*></td></tr><tr><td>Password:</td><td><input ~*login_passwordinput_properties*></td></tr><tr><td>Remember:</td><td><input ~*login_rememberinput_properties*></td></tr><tr><td colspan=\"2\"><input ~*login_registerbutton_properties*> <input ~*login_loginbutton_properties*></td></tr></form></table>";
    }
    }
    </script>
    And it works! Too bad it can't be split over multiple lines like PHP, this is rather unreadable...
    Last edited by Ge64; 12-18-2006 at 08:24 AM.

  • #10
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    It can, I told you how. Look at the +' stuff I used. I did only part of your code not all of it.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #11
    New Coder
    Join Date
    Dec 2006
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    It can, I told you how. Look at the +' stuff I used. I did only part of your code not all of it.
    Yeah I saw that but you would need to put every line between quotes and add +'s, unlike PHP where you can just put multiple lines between 2 quotes and it works. But oh well, it works Thanks for your help!

    Edit:

    Hey i just bumped into this:

    Break up a Code Line

    You can break up a code line within a text string with a backslash. The example below will be displayed properly:

    Code:
    document.write("Hello \
    World!")
    However, you cannot break up a code line like this:

    Code:
    document.write \
    ("Hello World!")
    Last edited by Ge64; 12-18-2006 at 08:39 AM.


  •  

    Posting Permissions

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