SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    When using the document.write, how can I position it and apply CSS?

    Here is my code:

    function executeUntil(end)
    {
    var i=0 ;
    while(i< 100)
    {
    if(i==end)
    break ;
    document.write("Javascript is fun");
    i++;
    }

    return(i);
    };
    (executeUntil(01));

    @ runtime, the output is Javascript is fun (obviously).

    My question is, how can I apply a CSS style to the document.write output, and how can I make a layer (well I know how to do that, lol), but say that I want the output to appear in that layer to control the appearance of the content? If I can do the latter with the layer, then applying a CSS style is simple.

    Thanks

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can output arbitrary html with write(), including css properties:

    Code:
    document.write("<p class='xyz'>Hello!</p>");

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    using document.write() is the old and bad way of doing things, you should be using the dom.

    Code:
     var newElm = document.createElement('div');
    newElm.id = 'someId';
    That does require more code but it has far less problems esp with different browsers.
    Or there is innerHTML if you need to insert lots of html at once.

    Code:
     refDiv.innerHTML = '<p>This is inserted with innerHTML</p>';
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •