Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript CDATA section

Recently, I was creating a module to add google remarketing tag to a webstore. I have prepared google_tag_params for different type of pages (product, category, cart etc.) according to the documentation. Everything went well until the client checked the page with Google Tag Assistant add-on to Chrome. It shows a warning for CDATA section. At first I didn't understand what he is talking about as the parameters were fine and I didn't receive any errors in the console. So I have checked the Google Tag Assistant and to my surprise it acts as follows.

For code:

<script type="text/javascript">
//<![CDATA[
var google_conversion_id = <?php echo $this->getConversionId();?>;
var google_conversion_label = '<?php echo $this->getConversionLabel();?>';
var google_custom_params = window.google_tag_params;
var google_remarketing_only = <?php echo $this->getRemarketingOnlyFlag();?>;
//]]> 
</script>

It shows warning "Missing CDATA comments" and points to the documentation https://support.google.com/tagassistant/answer/2978937?ref_topic=2947092#cdata_comments

But changing this to

<script type="text/javascript">
/*<![CDATA[*/
var google_conversion_id = <?php echo $this->getConversionId();?>;
var google_conversion_label = '<?php echo $this->getConversionLabel();?>';
var google_custom_params = window.google_tag_params;
var google_remarketing_only = <?php echo $this->getRemarketingOnlyFlag();?>;
/*]]> */
</script>

Makes the warning disappear.

So my question is this. Is there any difference between oneline comment and multiline comment in any browser? Is this only google tag assistant weird behaviour that does not recognize those comments?

like image 557
Zefiryn Avatar asked Nov 05 '13 07:11

Zefiryn


People also ask

What is JavaScript CDATA?

CDATA stands for Character Data and it tells the parser to copy the following code exactly (and not try to parse it.)

What is CDATA section?

A CDATA section is used to mark a section of an XML document, so that the XML parser interprets it only as character data, and not as markup. It comes handy when one XML data need to be embedded within another XML document.

Is CDATA deprecated?

Note: CDATA is now deprecated. Do not use. The CDATA Section interface is used within XML for including extended portions of text. This text is unescaped text, like < and & symbols.

Is CDATA needed?

A CDATA section is required if you need your document to parse as XML (e.g. when an XHTML page is interpreted as XML) and you want to be able to write literal i<10 and a && b instead of i&lt;10 and a &amp;&amp; b , as XHTML will parse the JavaScript code as parsed character data as opposed to character data by default.


2 Answers

Some html minifiers may have a problem during minification.

For example

<script type="text/javascript">
//<![CDATA[
    alert("Hello World");
//]]> 
</script>

become

<script type="text/javascript">//<![CDATA[alert("Hello World");//]]></script>

So /*<![CDATA[*/ is just a little bit more safe.

like image 64
Luca Rainone Avatar answered Oct 05 '22 01:10

Luca Rainone


No, there is no difference. Google Tag Assistent simply doesn't recognize the line-break terminated comments.

like image 26
Janus Troelsen Avatar answered Oct 05 '22 00:10

Janus Troelsen