Gopinath Perumal
Gopinath Perumal

Reputation: 2318

How to hide or remove comments in browser view page source?

I'm working on a site. It contains a lot of comments. When a user click the view page source in any browser, I want to hide or remove the comments from the HTML.

Is this possible? If possible, could someone say a way to achieve it.

Upvotes: 9

Views: 15956

Answers (7)

Chrispy
Chrispy

Reputation: 61

Easiest way to hide code from browser and page source, use php comments:

For quick one liner notes:

<?php //Hide this ?>

For blocks of code:

<?php /*

(html code to comment out here)

*/ ?>

Another advantage to using ?php as comments, is they're ...secret... you won't expose comments you want only for your team. Seeeee-cret

Upvotes: 6

Jrgal
Jrgal

Reputation: 1

Jsource view shows the source. You have no control over how the browser will render it.

If you don't want comments to show up when the user of the browser views the source, then don't put them in the source

Upvotes: 0

Mike
Mike

Reputation: 117

If you have your own webserver, you can use Google's plugin called PageSpeed that is available both for Apache and Nginx, one of it's many features is to remove your comments from the code both html and css.

Upvotes: 1

Andris
Andris

Reputation: 1442

At the moment I decided to use php to create html and jquery comments to hide them in view source

like

<input type="submit" value="Submit">
<?php //this is comment regarding input ?>

Possibly it affects performance... but found no other way

Regarding jquery one note.

//$('#upper_level_id0').css('color', 'red');<?php //works ?>
$('#upper_level_id'+index).remove();

In this example $('#upper_level_id'+index).remove(); does not work.

$('#upper_level_id0').css('color', 'red');<?php //works ?>
//$('#upper_level_id0').css('color', 'red');
<?php //works ?>
$('#upper_level_id'+index).remove();

But in this example all works. So conclusion that <?php comment better tos start in new line

Upvotes: 4

dkellner
dkellner

Reputation: 9926

I think the only workaround would be to open a new browser window and copy the html contents there using javascript - it will be the rendered code already so there you won't have any comments.

But it's rather unlikely that you need this. The simple way is not to output your comments on server side. Now there are a lot of options (output buffering plus minification seems to be the most reasonable).

Upvotes: 0

Shiva Avula
Shiva Avula

Reputation: 1836

Well you cant do that. But before you upload the html files to your server you can minify the source and upload them. But before uploading check if everything is working as expected or not. Try this website.

http://www.willpeavy.com/minifier/

Upvotes: 3

Quentin
Quentin

Reputation: 943605

The source view shows the source. You have no control over how the browser will render it.

If you don't want comments to show up when the user of the browser views the source, then don't put them in the source that your server delivers to the browser.

Upvotes: 2

Related Questions