Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I don't understand this Javascript comment-like code, can someone clarify this?

I am adding some Javascript generated by Google's Adsense on my page, but there is somethong which does not make sense to me:

<script type="text/javascript"><!--
    google_ad_client = "ca-pub-1234324234324324";
    /* LeftTop */
    google_ad_slot = "2343454355";
    google_ad_width = 200;
    google_ad_height = 200;
    //-->
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

Why does Google add !-- at the beginning of the first script and //-- at the end? What is the purpose of this code? What does it do? Is it really necessary?

like image 711
Jérôme Verstrynge Avatar asked Mar 15 '26 11:03

Jérôme Verstrynge


2 Answers

I saw this from here:

Why do we place JavaScript code inside comment fields ? It's for ensuring that the Script is not displayed by old browsers that do not support JavaScript. This is optional, but considered good practice.

like image 122
contactmatt Avatar answered Mar 17 '26 23:03

contactmatt


It's supposed to make browsers that don't support Javascript ignore everything inside the <script> block by making it look like a HTML comment. It's rarely used nowadays.

like image 42
James McLaughlin Avatar answered Mar 17 '26 23:03

James McLaughlin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!