Removal of comments in the HTML sent by the server

4

We all know the good old HTML comments:

<!-- Oi, eu sou o Goku! -->

The question is: Is there any way to exclude comments from the HTML that the server sends to the client? I think such a configuration is done at the server level (I am specifically below IIS) based programming are also welcome (I think something in the global routines: Global.asa - for ASP scripts - or Global.asax.cs - for ASP.NET pages - is also appropriate).

The need arose recently in the company I work for, as there are several HTML comments - therefore sent to the customer - with business details of the routines' operation, and these comments should have been made with their respective server side to be ignored during the build process.

    
asked by anonymous 05.03.2014 / 20:28

2 answers

4

For HTML code files

In case of using Asp.NET Use Asp.Net own comments and they will not appear in HTML.

<%-- É de mais de 8000!!!  --%>

For JS files

I use this script to concatenate and minify all the scripts in my application. More details about the DLL you can find at: link

        Response.ContentType = "application/x-javascript";
        string debug = Request.QueryString["debug"];
        string TakeScript = Request.QueryString["take"];

        List<string> JsFiles = new List<string>();

        JsFiles.Add("jquery-1.8.0.js");
        JsFiles.Add("jquery-ui.js");
        JsFiles.Add("meuScript.js");

        StringBuilder Output = new StringBuilder();

        foreach (var JsFile in JsFiles)
        {
            foreach (var JsLine in File.ReadAllLines(Server.MapPath("jsfiles/" + JsFile)))
            {
                if (!string.IsNullOrEmpty(JsLine.Replace(" ", string.Empty)))
                    Output.Append(JsLine + "\n");
            }
        }

        if (debug == "true")
        {
            Response.Write(Output.ToString());
        }
        else
        {
            CodeSettings Settings = new CodeSettings();
            Settings.MinifyCode = true;
            Settings.OutputMode = OutputMode.SingleLine;
            Minifier AjaxMinifier = new Minifier();
            string CompressedJavascript = AjaxMinifier.MinifyJavaScript(Output.ToString(), Settings);
            Response.Write(CompressedJavascript);
        }
    
06.03.2014 / 12:50
1

The solution I think you could implement would minify your HTML. This way, you "prepare" your HTML for requests coming from the client by cleaning up extra spaces, removing line breaks (this is not done inside JavaScript blocks) and, of course, removing comments.

Of course, this depends a lot on the architecture of your application. This type of routine is quite common when HTML consists of template files in which the variables will be embedded into placeholders and after that, there may be the minification routine that I spoke of.

Just one detail: you can do this with CSS and JS files as well. Uploading your site will be considerably faster:)

    
05.03.2014 / 23:11