Yet another Javascript question (outputting html tags)

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
Here's the code I have:

<script language=Javascript type="text/javascript">
<!--

var logout = "<a href="javascript:void(null)" onClick="return log_out()">logout<\/a>"; <-- bah.. it doesn't look like this..fusetalk parsed it for me.

if (!getCookie("currentUser")) { //not logged in
document.writeln("Not Logged In");
}
else {//logged in
document.write(logout);
}
//-->
</script>

It outputs this line (for ELSE): <a href="javascript:void(null)" onClick="return log_out()">logout</a>

This came out exactly the way I want it to be, but the problem is, I don't want it to be displayed as plain text - I want the browser to interpret this line as HTML code.

How do I do that?

Thanks.
 

igowerf

Diamond Member
Jun 27, 2000
7,697
1
76
You could put it in an invisible div tag instead and then set the tag to visible on the else case. It seems like you're trying to do with Javascript what people would normally do with a server side scripting language.

EDIT: I just tried your code and it seems to work for me. This is what I did:

<html>
<head>
<title></title>

<script language=Javascript type="text/javascript">
<!--

function bla() {

var logout = "<a href=\"javascript:void(null)\" onClick=\"return log_out()\">logout</a>";

document.write(logout);
}
//-->
</script>
</head>
<body onload="bla()">

</body>
</html>
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
Originally posted by: igowerf
You could put it in an invisible div tag instead and then set the tag to visible on the else case. It seems like you're trying to do with Javascript what people would normally do with a server side scripting language.

EDIT: I just tried your code and it seems to work for me. This is what I did:

<html>
<head>
<title></title>

<script language=Javascript type="text/javascript">
<!--

function bla() {

var logout = "<a href=\"javascript:void(null)\" onClick=\"return log_out()\">logout</a>";

document.write(logout);
}
//-->
</script>
</head>
<body onload="bla()">

</body>
</html>
This worked!

However, the code wouldn't validate now.. it gives two errors:

"end of file while parsing attributes" and "missing </a>"

Both errors are pointed out at the end of the html file :confused:


Edit: NVM found the solution. me = teh st00pid. I just needed to put a single quotation mark (') to enclose the string. Yay!! :)