Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I ensure nicely formatted HTML when I 'view source' on the browser?

When I 'view source' using different browsers my web pages seems to always have lots of whitespace and are badly formatted. I am developing a Java web application using JSPs.

How can I ensure that when I 'view source' I see nicely formatted HTML?

Edit:

When I say formatted I mean typically:

I get this kind of thing:

<div>




                     <p>some text</p>
     </div>

when I want:

<div>
      <p>some text</p>
</div>

In my JSPs (which use includes) everything is formatted nicely.

like image 850
Dan MacBean Avatar asked Oct 15 '11 14:10

Dan MacBean


2 Answers

View Source is only going to show whatever the page gives it in browsers that I know of, so if the HTML isn't formatted nicely, you won't see it formatted nicely.

Your best bet would be to use the developer tools in whatever browser you are in. The HTML is normally structed nicely in there when viewing the HTML. In Chrome or Internet Explorer, F12 should open the developer tools and I think F12 opens Firebug in Firefox if you have it installed.

Note: The source/HTML elements typically viewed by developer tools isn't exactly what the page spits out. They often show dynamic elements that were added through script and/or plugins.

like image 101
Doozer Blake Avatar answered Oct 24 '22 10:10

Doozer Blake


Altering generated html in a Java webapp is best done by a Servlet Filter. You can roll your own using whatever prettifying software you have to hand, or install one created by someone else, such as the JTidyFilter.

An advantage of this approach is that you can use the filter during development and remove it for production.

like image 25
Don Roby Avatar answered Oct 24 '22 10:10

Don Roby