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?
CDATA stands for Character Data and it tells the parser to copy the following code exactly (and not try to parse it.)
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.
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.
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<10 and a && b , as XHTML will parse the JavaScript code as parsed character data as opposed to character data by default.
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.
No, there is no difference. Google Tag Assistent simply doesn't recognize the line-break terminated comments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With