2

If in a Classic ASP page I write:

<%pippa="Camilla"%>
<!DOCTYPE html>

in the page source code I'll have a blank line before <!DOCTYPE html>.

How can I remove it? I new that I can write all in a single line like this:

<%pippa="Camilla"%><!DOCTYPE html>

but seems quite weird in the rest of the code. There is any way to solve it? And if yes, is it possible to use a solution in the web.config ?

What about using Dynamic Content Compression ?

Vixed
  • 3,429
  • 5
  • 37
  • 68
  • 1
    Dynamic Content Compression could help if it removes the white-space characters, but haven't tested it so my advice is give it a try and see how you get on. – user692942 Mar 15 '16 at 11:34

2 Answers2

2

You can't remove it. When the code is run on the server, the code within the <% %> tags is executed and any output placed in their position. All other characters within the ASP source are left as they are. So the line break after the initial closing %> tag is left as it is.

The only way around it is the way that you have yourself suggested. ie:

<%pippa="Camilla"%><!DOCTYPE html>

Not pretty, but it works.

mikeyq6
  • 1,138
  • 13
  • 27
2

This is one of the cruxes of server side code generation, the ASP processor only processes ASP code (code inside the in-line code blocks <% %>).

This means that any other HTML is left "as is" including special characters like carriage return and linefeed.

So when ASP processes your page it sees

⏎
<!DOCTYPE html>

⏎ Denotes Carriage Return, LineFeed

So as you already state in the question the only way to avoid this is placing the in-line code block on the same line.

<% pippa="Camilla" %><!DOCTYPE html>

The other approach is to break your HTML and your code assignment into different sections. I usually do this by placing my HTML inside it's own Sub / Function and pass or assign variables earlier in the page using an Init() Function, you still find yourself dealing with code like this though;

<%
Sub DisplayHTML()
%><!DOCTYPE html>
...
</html><% End Sub %>

Which isn't pretty, so I tend to ignore trailing new lines unless it's affecting the rendering of the page (IE compatibility mode etc).


Another way of dealing with HTML inside your code is to build HTML templates to load the HTML and replace values defined by place-holders.

See How to edit the html from ASP in a better way?

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175