Wrox Programmer Forums
|
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
 
Old April 6th, 2004, 04:01 PM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old April 6th, 2004, 04:16 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
 
Old April 6th, 2004, 04:47 PM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old April 6th, 2004, 04:53 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
 
Old April 6th, 2004, 04:57 PM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old April 6th, 2004, 05:11 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
 
Old April 7th, 2004, 09:10 AM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old April 7th, 2004, 09:50 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
 
Old April 7th, 2004, 09:54 AM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old April 7th, 2004, 10:22 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
CSS HELP:! CSS EXPANDIBLE BACKGROUND FOR TITLE phpuser2000 CSS Cascading Style Sheets 2 December 19th, 2007 12:36 AM
Rich's new CSS book: Beginning CSS 2nd Edition jminatel BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 0 June 15th, 2007 11:55 AM
send variable in function to another function schoolBoy Javascript How-To 6 March 3rd, 2007 09:16 AM
How to call javascript function from VB function vinod_yadav1919 VB How-To 0 February 13th, 2006 06:03 AM
retreive function/Line from macro or function? MikoMax J2EE 0 April 1st, 2004 04:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.