...

View Full Version : Simple script doesn't work



Ge64
12-18-2006, 06:15 AM
<!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

_Aerospace_Eng_
12-18-2006, 06:21 AM
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.

<!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

<!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.

Ge64
12-18-2006, 06:28 AM
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.

_Aerospace_Eng_
12-18-2006, 06:39 AM
Okay then I'm a little confused why not do something like

<?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>

Ge64
12-18-2006, 06:45 AM
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...

_Aerospace_Eng_
12-18-2006, 06:46 AM
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

.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
';

Ge64
12-18-2006, 06:50 AM
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.

_Aerospace_Eng_
12-18-2006, 06:58 AM
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?

Ge64
12-18-2006, 07:21 AM
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:


<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...

_Aerospace_Eng_
12-18-2006, 07:26 AM
It can, I told you how. Look at the +' stuff I used. I did only part of your code not all of it.

Ge64
12-18-2006, 07:29 AM
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 (http://www.w3schools.com/js/js_guidelines.asp):

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:


document.write("Hello \
World!")

However, you cannot break up a code line like this:


document.write \
("Hello World!")



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum