 |
| Javascript How-To Ask your "How do I do this with Javascript?" questions here. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 6th, 2004, 04:01 PM
|
|
Friend of Wrox
|
|
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
CSS to Function
Anyone out there know how I can attach a css style to a function that I am writing to a page?
Clay Hess
__________________
Clay Hess
|
|

April 6th, 2004, 04:16 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
Can you elaborate a bit about what you're trying to accomplish? What do you mean with "attach a css style to a function"?
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

April 6th, 2004, 04:47 PM
|
|
Friend of Wrox
|
|
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorry for the sparseness...Here is what I need to do. I have a function that I use to write dynamic text to the page. What I want to do is format that text so that it looks pretty like the rest of the pages. My function is defined in the head, although I might make a separate js file later, and I simply call the function in the page.
Clay Hess
|
|

April 6th, 2004, 04:53 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Right. If you write content to the page, can't you apply style or class information, like you normally would? e.g.:
document.write('<p class=\"CoolParagrapgh\">I am one cool looking paragraph</p>');
Does this help?
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

April 6th, 2004, 04:57 PM
|
|
Friend of Wrox
|
|
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The content is actually written in the function in the head section. I am just calling the function from the body and it writes it there. I cannot seem to apply formatting in the head.
Clay Hess
|
|

April 6th, 2004, 05:11 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
I am not sure I understand. Content in the header doesn't need to be formatted as it should be hidden (link tags, style tags etc)
If you call the function form the body, the stuff I showed you should work. The following page demonstrates a very simple example:
Code:
<!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>Document Write Test</title>
<style type="text/css">
.CoolParagraph
{
background-color: #ffffff;
color:#33ffcc;
}
</style>
<script type="text/javascript">
function WriteStyledStuff()
{
document.write('<p class=\"CoolParagraph\">I am one cool looking paragraph</p>');
}
</script>
</head>
<body>
<p>Normal Paragraph</p>
<script type="text/javascript">
WriteStyledStuff();
</script>
</body>
</html>
Isn't this what you're after?
[It's open for debate whether #33ffcc text on a #ffffff background is actually cool or not....;) ]
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

April 7th, 2004, 09:10 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
It might help if I show you my code. I should have done this earlier. Here ya go...
<head>
<title>AP Home</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="../lesson.css">
<style type="text/css">
<!--
@import url("/lesson.css");
-->
</style>
<script language="JavaScript">
var UserName
UserName=prompt("Please enter your user name.","");
</script>
<script language="JavaScript">
//declare function name that sets the current user
function sCurrentUser(UserName)
{
//state string(s) to write
document.write("Welcome, " + UserName + ". We hope you enjoy the training!");
return;
}
</script>
<script language="JavaScript">
var DocUrl=window.location.href;
var sLessonID="APLesson"
</script>
<script language="JavaScript">
Then there is some more stuff in the head...
Here is the body code...
<script language="JavaScript">
sCurrentUser(UserName);
</script>
So How do I format that?
Clay Hess
|
|

April 7th, 2004, 09:50 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
What are you referring to with that in "So How do I format that?"?
If I understand you correctly, my previous code should do the trick. Just write out style info or a class name in the sCurrentUser method, like I did in the WriteStyledStuff method.
Or am I missing something?
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

April 7th, 2004, 09:54 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Disregard the [i]. That was a mistake. I was trying to format my message.
I tried your previous code and it did not seem to work. I got an error message.
Clay Hess
|
|

April 7th, 2004, 10:22 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
What browser did you check this on?
Did you copy and paste my entire code. Was the line with document.write in it actually on one line?
if that doesn't help, can you post the error message? The code runs fine on my machine.....
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|
 |