Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript <!-- //--> are required?

Tags:

I just like to ask what the title says. The following string required into HTML script tags?

<!-- //--> 

If I don't use them what would happen?

like image 295
KodeFor.Me Avatar asked Nov 08 '11 11:11

KodeFor.Me


People also ask

Why is JavaScript required?

JavaScript has become integral to the Internet experience as developers build increased interaction and complexity into their applications. Search engines, ecommerce, content management systems, responsive design, social media and phone apps would not be possible without it.

Is JavaScript required for a website?

JavaScript is Must For Web Development. JavaScript is the most popular and most widely developed programming language ever existed. JavaScript is a must-have for Web developers, Front-end/ Back-end developers, Mobile / Desktop developers, Graphics / Game developers and even for Full Stack developers as well.

Is HTML required for JavaScript?

Can I learn JavaScript without knowing HTML and CSS? Yes, by learning Node JS which doesn't require HTML and CSS, unlike traditional web applications. Additionally, you can also learn JavaScript topics that can be executed independently in the JS compiler such as declarations, scopes, closures, ES6 classes, etc.


2 Answers

The worse thing that can happen is that your page is retrieved by a user agent that's not aware of the <script> tag and tries to parse your script block as regular HTML. JavaScript code will be handled as regular text and < symbols might interfere with the page markup. A forced example:

<script type="text/javascript"> if(a<del || a>b){     foo(); } </script> Lorem ipsum dolor sit amet. 

... could render as ugly deleted text:

if(ab){ foo(); } Lorem ipsum dolor sit amet.

Do these obsolete user agents exists? Oh, sure they do. Please note I've carefully avoided the word "browser". The question is not who's using Mosaic nowadays. It's that a your site can be read by a poorly-written PHP-powered regexp based parser.

Should you care? Well, probably not :)

like image 30
Álvaro González Avatar answered Nov 09 '22 03:11

Álvaro González


Not unless you are targeting browsers that predate the <script> element (i.e. Netscape 1 and friends). (Hint: You aren't).

If you don't use them, and a browser that old (so old it can't even cope with the HTTP Host header which is needed for sites that use virtual hosts) tries to access the site, then the content of the <script> element will be treated as text to be displayed.

Further reading: Comments and CDATA: The mysterious history of script and style in HTML

like image 81
Quentin Avatar answered Nov 09 '22 04:11

Quentin