document.write - CSS problem??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • marco

    document.write - CSS problem??

    Hi,

    I'm a Javascript Newby. But that doesn't discourage me at all.
    At this time I'm working on a little javascript-gimmick.

    A white browser page filled with white (invissible for the viewer)
    text. Every white text character has in case of a MouseOver an other
    (randomly given) change cursor of one of the 15 different browser
    cursors).


    I found some example scripts to change the cursor and tried to
    incoporate them with a text character generating loop and the
    document.write function. But somehow it doesn't work like it should
    be. I think that problem is that I can't get the the Java Script and
    the CSS working properly together.

    Anyone have some ideas or comments ?

    Thanks!

    here is the code where I'm working on

    <head>
    <title>Cursor Test Page</title>
    <style type="text/css">
    a.crosshair {cursor: crosshair;}
    a.default {cursor: default;}
    a.eastresize {cursor: e-resize;}
    a.help {cursor: help;}
    a.move {cursor: move;}
    a.northresize {cursor: n-resize;}
    a.northeastresi ze {cursor: ne-resize;}
    a.northwestresi ze {cursor: nw-resize;}
    a.pointer {cursor: pointer;}
    a.southresize {cursor: s-resize;}
    a.southeastresi ze {cursor: se-resize;}
    a.southwestresi ze {cursor: sw-resize;}
    a.text {cursor: text;}
    a.westresize {cursor: w-resize;}
    a.wait {cursor: wait;}
    </style>
    <SCRIPT LANGUAGE="JAVAS CRIPT">
    <!--
    var x = 0;
    var cursorArray = new Array ();
    cursorArray[0]= "crosshair" ;
    cursorArray[1]= "default" ;
    cursorArray[2]= "e-resize" ;
    cursorArray[3]= "help" ;
    cursorArray[4]= "move" ;
    cursorArray[5]= "n-resize" ;
    cursorArray[6]= "ne-resize" ;
    cursorArray[7]= "nw-resize" ;
    cursorArray[8]= "pointer" ;
    cursorArray[9]= "s-resize" ;
    cursorArray[10]= "se-resize" ;
    cursorArray[11]= "sw-resize" ;
    cursorArray[12]= "text" ;
    cursorArray[13]= "w-resize" ;
    cursorArray[14]= "wait" ;

    do {
    var R = Math.floor(math .random() *15);
    document.write( "<a href="test" class=" + cursorArray[R] + ">A</a>");
    x = x+1;
    } while (x<900)
    //-->
    </SCRIPT>
    </head>
  • Harag

    #2
    Re: document.write - CSS problem??

    On 31 Jul 2004 02:39:23 -0700, marco@mptheunis sen.nl (marco) wrote:
    [color=blue]
    >Hi,
    >
    >I'm a Javascript Newby. But that doesn't discourage me at all.
    >At this time I'm working on a little javascript-gimmick.
    >
    >A white browser page filled with white (invissible for the viewer)
    >text. Every white text character has in case of a MouseOver an other
    >(randomly given) change cursor of one of the 15 different browser
    >cursors).
    >
    >
    >I found some example scripts to change the cursor and tried to
    >incoporate them with a text character generating loop and the
    >document.wri te function. But somehow it doesn't work like it should
    >be. I think that problem is that I can't get the the Java Script and
    >the CSS working properly together.
    >
    >Anyone have some ideas or comments ?
    >[/color]

    ok, heres my comments....

    [color=blue]
    >Thanks!
    >
    >here is the code where I'm working on
    >
    > <head>
    > <title>Cursor Test Page</title>
    > <style type="text/css">
    > a.crosshair {cursor: crosshair;}
    > a.default {cursor: default;}
    > a.eastresize {cursor: e-resize;}
    > a.help {cursor: help;}
    > a.move {cursor: move;}
    > a.northresize {cursor: n-resize;}
    > a.northeastresi ze {cursor: ne-resize;}
    > a.northwestresi ze {cursor: nw-resize;}
    > a.pointer {cursor: pointer;}
    > a.southresize {cursor: s-resize;}
    > a.southeastresi ze {cursor: se-resize;}
    > a.southwestresi ze {cursor: sw-resize;}
    > a.text {cursor: text;}
    > a.westresize {cursor: w-resize;}
    > a.wait {cursor: wait;}
    > </style>
    > <SCRIPT LANGUAGE="JAVAS CRIPT">[/color]


    don't use LANGUAGE="JAVAS CRIPT" use type="text/javascript" and best
    to do the tag in lowercase

    <script type="text/javascript">


    [color=blue]
    > <!--[/color]

    commenting out your script isn't really needed these days, most people
    (if not all) ain't using old browsers.
    [color=blue]
    > var x = 0;
    > var cursorArray = new Array ();
    > cursorArray[0]= "crosshair" ;
    > cursorArray[1]= "default" ;
    > cursorArray[2]= "e-resize" ;
    > cursorArray[3]= "help" ;
    > cursorArray[4]= "move" ;
    > cursorArray[5]= "n-resize" ;
    > cursorArray[6]= "ne-resize" ;
    > cursorArray[7]= "nw-resize" ;
    > cursorArray[8]= "pointer" ;
    > cursorArray[9]= "s-resize" ;
    > cursorArray[10]= "se-resize" ;
    > cursorArray[11]= "sw-resize" ;
    > cursorArray[12]= "text" ;
    > cursorArray[13]= "w-resize" ;
    > cursorArray[14]= "wait" ;[/color]

    Make sure your stings above match the CSS classnames... (which they
    dont in alot of the cases

    eg
    CSS = a.southeastresi ze
    JS = cursorArray[10]= "se-resize" ;

    Not the same names...

    [color=blue]
    >
    > do {
    > var R = Math.floor(math .random() *15);[/color]

    math.random() is wrong should be
    Math.random() - note the capital M.

    [color=blue]
    > document.write( "<a href="test" class=" + cursorArray[R] + ">A</a>");[/color]

    I have always found it best to use single quotes for strings in
    javascript and double quotes for HTML eg

    document.write( '<a href="test" class="' + cursorArray[R] + '">A</a>');

    note that after class= that is a double quote followed by a single
    quote, always put double quotes around HTML values. eg:

    <img border="0" width="10" height="20" src="mypic.gif" >

    [color=blue]
    > x = x+1;[/color]


    for adding 1 to variables in javascript use

    x++

    others you can use are

    x += 1
    x -= 1
    x *= 3

    etc



    [color=blue]
    > } while (x<900)[/color]

    spaces makes code easier to read

    while (x < 900)
    [color=blue]
    > //-->[/color]

    again, no real need to comment out
    [color=blue]
    > </SCRIPT>[/color]

    again, lowercase[color=blue]
    > </head>[/color]


    If your learning by a good book, I recommend the following:

    Javascript: The Definitive guide
    by David Flanagan
    publisher: O'Reilly
    ISBN: 0-596-00048-0


    HTH

    Al


    Comment

    • Blue Raja

      #3
      Re: document.write - CSS problem??

      Not to be outdone by Howard Dean, Harag bellowed
      epsmg095m38vjuv eiqd410n9go4tu3 fote@4ax.com:[color=blue][color=green]
      >> x = x+1;[/color]
      >
      > for adding 1 to variables in javascript use
      >
      > x++[/color]

      The unary increment is quite handy. IMO it looks neater too.
      Does Javascript support pre-use unary increment/decrement, eg ++i?
      [color=blue]
      > others you can use are
      >
      > x += 1
      > x -= 1
      > x *= 3[/color]

      Which are equivalant to
      x = x + 1
      x = x - 1
      x = x * 3
      respectively, in case the OP isn't familiar with the notation.

      I also like the alternate condition statement
      <boolean_exp> ? <true_action> : <false_action >;
      which is equivalent to
      if (<boolean_exp> ) <true_action> ; else <false_action >;

      --

      Jason, aka The Blue Raja


      Comment

      • Michael Winter

        #4
        Re: document.write - CSS problem??

        On 31 Jul 2004 02:39:23 -0700, marco <marco@mptheuni ssen.nl> wrote:

        [snip]
        [color=blue]
        > I think that problem is that I can't get the the Java Script and
        > the CSS working properly together.
        >
        > Anyone have some ideas or comments ?[/color]

        [snipped code]

        In addition to Harag's comments, it should also be noted that your script
        will produce invalid HTML. Once the script has executed, the HEAD block of
        the document will contain 900 A elements. I shouldn't need to tell you
        that A elements cannot occur within HEAD. The simplest solution is to move
        the SCRIPT block into a block-level element (DIV, P, etc) inside the
        document BODY. The alternative is to wrap the script in a function and
        call that function from within a SCRIPT placed inside a block-level
        element. That is:

        <!-- HTML and DOCTYPE -->
        <head>
        <script type="text/javascript">
        function myFunction() {
        // ... Your script ...
        }
        </script>
        </head>

        <body>
        <div>
        <script type="text/javascript">
        myFunction();
        </script>
        </div>
        <!-- Remaining document -->

        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply

        Comment

        • Michael Winter

          #5
          Re: document.write - CSS problem??

          On Sat, 31 Jul 2004 21:25:22 +1000, Blue Raja
          <the_blue_raja. spamless@iprimu s.com.au> wrote:
          [color=blue]
          > Not to be outdone by Howard Dean, Harag bellowed
          > epsmg095m38vjuv eiqd410n9go4tu3 fote@4ax.com:[/color]

          [snip]
          [color=blue][color=green]
          >> x++[/color]
          >
          > The unary increment is quite handy. IMO it looks neater too.
          > Does Javascript support pre-use unary increment/decrement, eg ++i?[/color]

          Yes, JavaScript supports both pre- and post-increment/decrement.

          [snip]
          [color=blue]
          > I also like the alternate condition statement
          > <boolean_exp> ? <true_action> : <false_action >;
          > which is equivalent to
          > if (<boolean_exp> ) <true_action> ; else <false_action >;[/color]

          I would argue that it should be used sparingly, though. I've seen examples
          where several conditional operators are used nested, and with no
          parentheses. It quickly becomes unreadable without careful formatting.
          That said, it certainly can be very useful.

          Mike

          --
          Michael Winter
          Replace ".invalid" with ".uk" to reply

          Comment

          • marco

            #6
            Re: document.write - CSS problem??

            >[color=blue]
            >
            > If your learning by a good book, I recommend the following:
            >
            > Javascript: The Definitive guide
            > by David Flanagan
            > publisher: O'Reilly
            > ISBN: 0-596-00048-0
            >
            >
            > HTH
            >
            > Al[/color]

            Hahahahaha.
            Guess what I just bought in the bookstore some 10 minutes ago!
            Thanks, I'll take a look at your comments and see if I (with the new
            O'Reilly in my possession) can work it out.

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: document.write - CSS problem??

              Harag wrote:[color=blue]
              > On 31 Jul 2004 02:39:23 -0700, marco@mptheunis sen.nl (marco) wrote:[color=green]
              >> <!--[/color]
              >
              > commenting out your script isn't really needed these days, most people
              > (if not all) ain't using old browsers.[/color]

              s/old/this broken/

              The "script" element was introduced in HTML 3.2 and its predecessor,
              HTML 2.0, has been marked obsolete. So only borken UAs will display
              the content of this element.


              PointedEars

              Comment

              • marco

                #8
                Re: document.write - CSS problem??

                OK. It works!! :-)

                The main problem was the quote issue.
                I needed an extra set of quotes in the -- class=" + cursorArray[R] +
                "-- part.
                Now it is like this: -- class="' + cursorArray[R] + '" And it works
                fine.
                Indeed it is a good advice to use the double quotes for distincting
                the HTML and the single quotes for the JavaScript strings. It's saves
                a lot of problems.

                Thanks a lot

                Marco

                Comment

                • marco

                  #9
                  Re: document.write - CSS problem??

                  Yes you're right. I wasn't at that point yet. The idea was to put the
                  text characters in a layer (<div>) so I can also can choose the
                  dimensions and coordinates. Now the script is actually doing something
                  I'm going to clean it up.
                  [color=blue]
                  > I shouldn't need to tell you that A elements cannot occur within HEAD.[/color]

                  Well, I have to say it does work. Even with 2400 characters. Only it
                  takes some time to load (±8 sec with 1024 ADSL).

                  Marco

                  Comment

                  • Michael Winter

                    #10
                    Re: document.write - CSS problem??

                    On 31 Jul 2004 12:31:08 -0700, marco <marco@mptheuni ssen.nl> wrote:
                    [color=blue]
                    > Yes you're right. I wasn't at that point yet. The idea was to put the
                    > text characters in a layer (<div>) so I can also can choose the
                    > dimensions and coordinates. Now the script is actually doing something
                    > I'm going to clean it up.[/color]

                    Just pointing it out.
                    [color=blue][color=green]
                    >> I shouldn't need to tell you that A elements cannot occur within HEAD.[/color]
                    >
                    > Well, I have to say it does work. [...][/color]

                    There are plenty of things that "work"[1] in HTML, but that doesn't mean
                    you should do them. :P

                    Good luck,
                    Mike


                    [1] I say "work" because invalid HTML can have some quite unexpected
                    consequences.

                    --
                    Michael Winter
                    Replace ".invalid" with ".uk" to reply by e-mail

                    Comment

                    • Blue Raja

                      #11
                      Re: document.write - CSS problem??

                      Not to be outdone by Howard Dean, Michael Winter bellowed
                      <opsbz2hre1x13k vk@atlantis>:[color=blue][color=green][color=darkred]
                      >>> x++[/color]
                      >>
                      >> The unary increment is quite handy. IMO it looks neater too.
                      >> Does Javascript support pre-use unary increment/decrement, eg ++i?[/color]
                      >
                      > Yes, JavaScript supports both pre- and post-increment/decrement.[/color]

                      Good to know, thanks :o)
                      [color=blue][color=green]
                      >> I also like the alternate condition statement
                      >> <boolean_exp> ? <true_action> : <false_action >;
                      >> which is equivalent to
                      >> if (<boolean_exp> ) <true_action> ; else <false_action >;[/color]
                      >
                      > I would argue that it should be used sparingly, though. I've seen
                      > examples where several conditional operators are used nested, and
                      > with no parentheses. It quickly becomes unreadable without careful
                      > formatting. That said, it certainly can be very useful.[/color]

                      I agree. I usually only use it for simple things.
                      I find it particularly useful when I'm toggling a CSS value, say text color,
                      eg
                      mytext.style.co lor = mytext.style.co lor=="#ff0000" ? "#0000ff" :
                      "#ff0000";

                      --

                      Jason, aka The Blue Raja


                      Comment

                      • Harag

                        #12
                        Re: document.write - CSS problem??

                        On 31 Jul 2004 08:52:13 -0700, marco@mptheunis sen.nl (marco) wrote:
                        [color=blue][color=green]
                        >>
                        >>
                        >> If your learning by a good book, I recommend the following:
                        >>
                        >> Javascript: The Definitive guide
                        >> by David Flanagan
                        >> publisher: O'Reilly
                        >> ISBN: 0-596-00048-0
                        >>
                        >>
                        >> HTH
                        >>
                        >> Al[/color]
                        >
                        >Hahahahaha.
                        >Guess what I just bought in the bookstore some 10 minutes ago!
                        >Thanks, I'll take a look at your comments and see if I (with the new
                        >O'Reilly in my possession) can work it out.[/color]


                        When I first picked the book up a couple of weeks (recommend to be by
                        others) I wasn't sure at first if its the one I really wanted.... but
                        after a couple of weeks reading, I understand things a LOT better now,
                        things like "prototype" , and how functions can be assigned to
                        variables etc (and why) and it looks an excellent reference source
                        and easy to find things that you might want. eg how do you get the
                        text that a user has typed in a text box... is it "element.te xt" or
                        "element.value" , with a quick look you have the answer.

                        Al.

                        Comment

                        • Harag

                          #13
                          Re: document.write - CSS problem??

                          On 31 Jul 2004 10:24:49 -0700, marco@mptheunis sen.nl (marco) wrote:
                          [color=blue]
                          >OK. It works!! :-)
                          >
                          >The main problem was the quote issue.
                          >I needed an extra set of quotes in the -- class=" + cursorArray[R] +
                          >"-- part.
                          >Now it is like this: -- class="' + cursorArray[R] + '" And it works
                          >fine.
                          >Indeed it is a good advice to use the double quotes for distincting
                          >the HTML and the single quotes for the JavaScript strings. It's saves
                          >a lot of problems.[/color]

                          Yes, this was the Main problem with your code, also some of the
                          classnames in "cursorArra y[R]" didn't match the names in the <style>

                          Glad you have it working now :)

                          Al.


                          Comment

                          • Lasse Reichstein Nielsen

                            #14
                            Re: document.write - CSS problem??

                            "Blue Raja" <the_blue_raja. spamless@iprimu s.com.au> writes:
                            [color=blue]
                            > I also like the alternate condition statement
                            > <boolean_exp> ? <true_action> : <false_action >;
                            > which is equivalent to
                            > if (<boolean_exp> ) <true_action> ; else <false_action >;[/color]

                            It's not really equivalent, more like parallel.

                            In Javascript, as in most langauges, there are (at least) two
                            different syntactic categories: statements and expressions.

                            Expressions are evaluated and their result is a value. Examples
                            are "2+2" or "Math.sin(Math. asin(Math.PI))" . You can combine
                            expressions into other expressions using operators or functions.

                            Statements are executed and their result is a change of state. They
                            do not have a value. Examples are "for(...){. ..}" and
                            "if(...){.. .}". You can put several statements together by
                            juxtaposition, and you can group them with "{" and "}".

                            The "if" statement is a conditional statement. It is a choice between
                            one of two statments, based on the value of an expression. Since doing
                            nothing is a (trivial) change of state, you can even omit one of the
                            statements (the "else" branch).

                            The "?:" operator defines a conditional expression. It is a choice
                            between one of two expressions, based on the value of a third. Since
                            an expression must have a value, you can not omit one of the expressions.


                            A "conditiona l whatever" is a general concept in programming. For most
                            syntactic categories, it makes sense to have an either/or choice
                            between two different possibilities. Javascript, as most C-based
                            languages only have conditional statements and expressions (the only
                            other syntactic category in C was the declaration, which is harder
                            to make conditional).

                            In general, a "conditiona l whatever" is itself a "whatever", and it
                            contains two "whatever"' s and a condition expression that chooses
                            between them. The "if" statement and the "?:" expression are examples
                            of this general concept.

                            The switch/case statement is a generalization, where there are more
                            than two different statements to choose between.

                            /L
                            --
                            Lasse Reichstein Nielsen - lrn@hotpop.com
                            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                            'Faith without judgement merely degrades the spirit divine.'

                            Comment

                            • Blue Raja

                              #15
                              Re: document.write - CSS problem??

                              Not to be outdone by Howard Dean, Lasse Reichstein Nielsen bellowed
                              d62bxgmg.fsf@ho tpop.com:[color=blue][color=green]
                              >> I also like the alternate condition statement
                              >> <boolean_exp> ? <true_action> : <false_action >;
                              >> which is equivalent to
                              >> if (<boolean_exp> ) <true_action> ; else <false_action >;[/color]
                              >
                              > It's not really equivalent, more like parallel.
                              >
                              > In Javascript, as in most langauges, there are (at least) two
                              > different syntactic categories: statements and expressions.[/color]
                              <snip>

                              All quite right, and an oversight on my part for implying the two were
                              equal.
                              Really the first should be more like
                              <boolean_exp> ? <true_value> : <false_value> ;

                              This is why I don't do as well as I could at exam time -- too much coding,
                              not enough theory ;o)

                              --

                              Jason, aka The Blue Raja


                              Comment

                              Working...