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?
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?
Use the javascript hex entity for a non-breaking space:
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.Code:var test=document.createTextNode('\xa0test');
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:
The output would be:Code:<script type="text/javascript"> onload=function(){ var test=document.createTextNode(' test'); document.body.appendChild(test); } </script> </head> <body> test </body>
test test
- the space would be preserved, but with:
the output is:Code:<script type="text/javascript"> onload=function(){ var test=document.createTextNode(' test'); document.body.appendChild(test); } </script> </head> <body> <div>test</div> </body>
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:
with no script at all.HTML Code:<body> <div>test</div> test </body>
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
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!
Thanks for the help. The \xa0 worked exactly how I wanted.
Bookmarks