Removing comments in the HTML sent by the server

Asked

Viewed 231 times

4

We all know the good old HTML comments:

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

The point is: there is some way to delete HTML comments that the server sends to the client? I think such configuration is done at the server level (I am specifically below IIS), but programming-based solutions are also welcome (I think something in the global routines: Global.asa - for ASP scripts - or Global.asax.cs - for ASP.NET pages - be also suitable).

The need arose recently in the company where I work, because there are several HTML comments - sent therefore to the client - with negotiation details of the functioning of the routines, and these comments should have been made with their respective versions server side, to be ignored during the build process.

  • I don’t think there’s any solution as you imagined. In my opinion the simplest alternative would be to convert such comments into comments of the server-side type using for example Visual Studio’s Find and Replace option.

  • @iuristona at first thought about it too, but I decided to ask anyway! If it is something that can be solved by the server, it would be one less concern when indoctrinating the team. Anyway, thank you for participating

  • Even if there is a way to process and remove on the server (an HTML parser would do that), I don’t think it’s worth burdening all requests with this filtering. The ideal would be some kind of script that actually changes the source files, completely removing the comments or converting them to a server-side version.

  • Good argument, @bfavaretto! I had not thought from this perspective.

  • Of course it is possible! In PHP, for example, just use the function ob_start to pick up the content of the page before it is sent and preg_replace to remove HTML comments. But in ASP I don’t know how to do it. Search here: https://www.google.com/search?q=asp+ob_start+like

  • 1

    The @bfavaretto suggestion is the best one. Pre-process all your content by removing comments. PS: Someone please edit the post. The correct comment would be <!-- Oi, eu sou o Goku! -->.

  • 1

    @Onosendai done ;-)

  • @Tiagocésaroliveira perfect, thank you. =)

  • @Gabrielsantos agrees with you, but I also agree that the ideal would be to pre-process the files to avoid unnecessary processing. I am already here working on a regex that allows me to work with a parser in order to solve this. When finished, I share here the implementation.

  • @Tiagocésaroliveira Yes, but if you have many files, will give a job to do it in hand. With a regex, the process is automatic. In addition, it is possible to save Bandwidth by removing spaces and line breaks. I always do this by leaving HTML in one line. You know.

  • @Gabrielsantos what I’m trying to do is an executable that reads file to file, locates the comments via regex and turns them into server side comments. This way I save pre-processing before serving the page

Show 6 more comments

2 answers

4


For HTML code files

In case you use Asp.NET Use the Asp.Net own comments and they will not appear in HTML.

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

For the JS files

I use this script to concatenate and minify all the scripts of my application. More details about the DLL you can find at : https://ajaxmin.codeplex.com/

        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);
        }
  • Perfect for ASP.NET solutions, but ignores support files (js, css). It is also necessary to change the end of the tag, from --> for --%>.

  • @Onosendai For these I recommend using the DLL found in: https://ajaxmin.codeplex.com/. I use it on my company website, grant all scripts and minification.

  • Interesting solution, run at each build? A while ago I implemented an HTTP Module that performed the same work and cached the results.

1

The solution I think you could implement would be minify your HTML. That way, you "prepare" your HTML for client requests by clearing extra spaces, removing line breaks (this just isn’t done within 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 variables will be incorporated into placeholders and after that, there could be the minification routine that I talked about.

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

  • Unfortunately it is not possible to do this because pages need preprocessing (in the case of ASP scripts)...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.