views:

43

answers:

3

I am using jQuery to .load() the contents of a CSS file into a div. But when the contents of that file are loaded, all the line breaks, tabs, etc. are gone. Is there a way to do a load of a css file while still preserving the line breaks?

Thanks in advance!

+5  A: 

Instead of a <div> just use a <pre> element to preserve formatting, you can .load() into it the exact same way.

The default behavior of HTML is to display without line-breaks, extra white-space, etc (except in a <pre></pre>), from the HTML4 spec:

For all HTML elements except PRE, sequences of white space separate "words" (we use the term "word" here to mean "sequences of non-white space characters"). When formatting text, user agents should identify these words and lay them out according to the conventions of the particular written language (script) and target medium.

Nick Craver
Geez, why didn't I think of that? That worked perfectly. Thanks!
Gary
Gary, you might want to consider accepting this answer.
Nick Presta
I hadn't seen the "accept" function (I'm pretty new here) but have found it now. Thanks for the tip.
Gary
A: 

I do not get this issue myself:

$('<textarea>').load('./styles/style.css').appendTo('body');

I insert that into the page:

http://www.balupton.com/sandbox/jquery-sparkle/demo/

And it displays as expected.

The issue probably lays with the line endings used. The web uses "\n" while some versions of mac use "\r", and windows uses "\r\n".

Or if you are trying to display it in a DIV, you will have to use the CSS white-space: pre; to show the line spaces without converting them into BRs.

Edit: forgot the dash in white-space.

balupton
This is really useful also, thanks very much.
Gary
Thanks mate, glad I could help :-). Perhaps you would like to mark my answer as useful by voting it up (clicking the up arrow next to the answer), that will show it helped and give me a virtual pat on the back ;-)
balupton
A: 

You can use a jQuery plugin to syntax highlight too: http://alexgorbatchev.com/SyntaxHighlighter/

Matt Williamson