Results 1 to 4 of 4

Thread: createTextNode()

  1. #1
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default createTextNode()

    How can you use document.createTextNode() and have it retain blank spaces (" ")? My FF1 will chop the extra whitespace, while IE6 won't. Replacing the spaces with " " doesn't work either. I don't want to use "pre" or "innerHTML". Any ideas?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Use the javascript hex entity for a non-breaking space:

    Code:
    var test=document.createTextNode('\xa0test');
    That is the only way I know of to do exactly what you propose. There are other avenues to get the effect desired, depending upon just what that is, like appending a span with padding which itself has had the text node appended to it.

    Notes: FF is just formating the text in the same manner as it would if the element and text were hard coded. Like if you had:

    Code:
    <script type="text/javascript">
    onload=function(){
    var test=document.createTextNode(' test');
    document.body.appendChild(test);
    }
    </script>
    </head>
    <body>
    test
    </body>
    The output would be:

    test test

    - the space would be preserved, but with:

    Code:
    <script type="text/javascript">
    onload=function(){
    var test=document.createTextNode(' test');
    document.body.appendChild(test);
    }
    </script>
    </head>
    <body>
    <div>test</div>
    </body>
    the output is:

    test
    test

    Since the space in the text node is now leading in its parent, it is ignored, just as it would be if you had:

    HTML Code:
    <body>
    <div>test</div>
     test
    </body>
    with no script at all.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Try .style.whiteSpace = "pre";
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Thanks for the help. The \xa0 worked exactly how I wanted.

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
  •