最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - can we hide javascript comment in page when it renders ? - Stack Overflow

programmeradmin4浏览0评论

question may seems to be silly, just i am curious about this. suppose in my html or aspx page i added ment for some javascript code, when the page loads, it renders as the page it is inlcuding ments. as i thought that ment will be ignored from the execution. is it same apply for rendering ? will it be possible.

  1. Comment are used to describe what the code does. so below i wrote " this for replacing image ". can we hide from rendering.

P.S : As this question has no special reason, just i am curious . whether it is possible or not.

$(".SomeID").slice(5, ExpandLength).each(function() {
                this.src = this.src.replace("some.gif", "som1.gif"); // this for replacing image 
            });

question may seems to be silly, just i am curious about this. suppose in my html or aspx page i added ment for some javascript code, when the page loads, it renders as the page it is inlcuding ments. as i thought that ment will be ignored from the execution. is it same apply for rendering ? will it be possible.

  1. Comment are used to describe what the code does. so below i wrote " this for replacing image ". can we hide from rendering.

P.S : As this question has no special reason, just i am curious . whether it is possible or not.

$(".SomeID").slice(5, ExpandLength).each(function() {
                this.src = this.src.replace("some.gif", "som1.gif"); // this for replacing image 
            });
Share Improve this question asked Mar 15, 2012 at 13:10 Ravi GadagRavi Gadag 15.9k5 gold badges59 silver badges85 bronze badges 1
  • 2 If you use a minifier it will strip them out. – Paul Commented Mar 15, 2012 at 13:13
Add a ment  | 

10 Answers 10

Reset to default 3

Look at YUI to minimise your javascript. You will need to do some post-processing using a tool like that when you deploy to remove ments. Otherwise, you're just sticking the same file up and people can see what you do (just like html or anything else that is served to the user directly).

Look at this question for a good answer:

YUI remove javascript ments

PS I'm assuming your javascript is in a .js or a .html file, otherwise (if in an asp, aspx or php file) the other answers about using server side ments are the best way to do it.

Still.. your javascript should be in a js file. It's much easier to manage :)

Rendering is a term that is applied to two distinct stages of the process.

  1. When the ASP generates some output to send in the HTTP response
  2. When the browser parses the HTML and creates a visible representation for the user

A ment in JavaScript is, as far as the ASP is concerned, just another piece of text. It sees no difference between that and a piece of HTML or any other part of the JavaScript. Therefore, during stage 1, it will be passed through to the browser.

When the browser receives the response it processes the HTML and JavaScript and generates the webpage for the user to see. When it is executing the JavaScript the ment has special significance (i.e. it should be ignored) so it isn't executed.

It is still part of the source code though.

To prevent that you would have to run the JavaScript through a JavaScript parser on the server, remove the ments, then output whatever remained.

It is a mon practise to keep JavaScript in external files and to process them using a minifier (a tool that removes unneeded text from the JS, such as ments and white space) before deploying the site to the server.

Front-end ments (JS, HTML, CSS etc) cannot be hidden from the renderer, but ments, e.g. <%-- --%> are not rendered in the source

It is good to ment code, but when javascript is used in production it should be as pact as possible. Your best bet is to minify the code for production. Search for "minify java script" on the internet.

You'd need to manually transform the javascript using an external library, probably at build time. E.g. Using the YUI Compressor library.

http://developer.yahoo./yui/pressor/

Unfortunately Javascript can't modify itself so there's no inherent way in JS to remove ments from the Javascript without an external library.

If you minify the code with a build script not only will these ments be removed, but your code will also have no white-spaces (other than in strings) and will render faster in the browsers DOM. This is actually best practice. Checkout

HTML 5 Boiler Plate Build Script, the YUI Compress or the NetTuts Builder app.

Several tools are freely available to minify JavaScript, including the Closure Compiler, JSMin or the YUI Compressor.

You can create a build process that uses these tools to minify and rename the development files and save them to a production directory.

The experts remend minifying any JS files that are 4096 bytes or larger in size.

You should see a benefit for any file that can be reduced by 25 bytes or more (less than this will not result in any appreciable performance gain).

Reference: [http://code.google./speed/page-speed/docs/payload.html#MinifyJS](Minimize payload size)

Pass your ASP code through this minifier to see if it solves your problems of ments in JavaScript:

http://prettydiff./?m=minify&html

I work with ASP core, have moved the JS to site.js and use minifying.
But I still have some JS in the view and don't want, that my ments are showed in GC.
In Asp core, you can use:

@* Your hidden ment *@
instead of
// Your visible ment

If you surround a block of ments with script tags they will not print to the screen

发布评论

评论列表(0)

  1. 暂无评论