I will publish a full HTML page.There are some inline JavaScript code in page. Visit this page, inline JavaScript doesn't work. I view page source,find inline JavaScript is Below:

<script type="text/javascript"><![CDATA[
]]><!--//--><![CDATA[// ><!--
alert('test');
//--><!]]><![CDATA[
]]></script>

I don't understand why drupal  add '<![CDATA[]]><!--//--><![CDATA[// ><!--'  in there, it makes inline JavaScript doesn't work. If drupal add '<!--//--><![CDATA[// ><!--'  in inline JavaScript,it works. In addition ,I find drupal's inline JavaScript codes are added correct CDATA.
Below:
<script type="text/javascript"> 
<!--//--><![CDATA[//><!--
jQuery.extend(Drupal.settings, {"basePath":"\/","pathPrefix":"","ajaxPageState":{"theme":"javy","theme_token":"Kj-.....
//--><!]]>
</script> 

Anybody help me?

Thanks a lot~

Comments

jaypan’s picture

The CDATA won't stop your script from working, and in fact will keep it working in some situations where it wouldn't (which is why it's there). There is likely an error in some javascript somewhere.

Install the firebug add-on for firefox, and test it there. Firebug will show you all the javascript errors and help you track down your issue.

edit: it may also help if you show us how you are adding your javascript. You may be adding it incorrectly.

Contact me to contract me for D7 -> D10/11 migrations.

Juno’s picture

Thanks for commenting. I know CDATA's meaning,but drupal adds incorrectly CDATA. A new page put

<script type="text/javascript"><![CDATA[
]]><!--//--><![CDATA[// ><!--
alert('test');
//--><!]]><![CDATA[
]]></script>

It don't alert.

jaypan’s picture

You're right, that doesn't look right. So again, how are you adding your javascript?

Contact me to contract me for D7 -> D10/11 migrations.

mmatiaschek’s picture

Did you solve the Problem? I have a similar problem when trying to add inline Javascript. Adding JS Libraries in the header is no problem. I tried various methods, more or less like this:

drupal_add_js("jQuery(document).ready(function () { 
       jQuery("#myPageFlip").jPageFlip({
        width: "480", 
        height: "480",
       });
  });', 'inline');

or this:

drupal_add_js('(function ($) {
  Drupal.behaviors.[myPageFlip] =  {
    attach: function(context, settings) {

       $("#myPageFlip").jPageFlip({
        width: "480", 
        height: "480",
         // weitere parameter
       });

    }
  };
})(jQuery);', 'inline');

from the author of the associated js library i got this snippet:

     $(document).ready(function(){
       $('#myPageFlip').jPageFlip({
        width: "Bildbreite", 
        height: "Bildhöhe",
         // weitere parameter
       });
     });

in script tags of course.

I would appreciate any hint or help...

jaypan’s picture

...it would help if you also told us what the problem is, and what errors you are receiving.

Contact me to contract me for D7 -> D10/11 migrations.

mmatiaschek’s picture

Thank you for your prompt reply.

I just found out in FireBug that my JavaScript actually seems to be executed. For some other reason it doesn´t work. Maybe there is a problem with the way drupal is adding the class attributes myPageFlip and jPageFlip to the divs?

What i am trying to do is getting the jPageFlip JavaScript to run.
As you can see on http://www.jpageflip.de/ there is a library needed and the plugin is activated the following way:

     $(document).ready(function(){
       $('#myPageFlip').jPageFlip({
        width: "Bildbreite", 
        height: "Bildhöhe",
         // weitere parameter
       });
     });

I can add the library as a file without problems through a PHP Code text format custom Block and am trying to add the needed inline code the same way:

<?php 
drupal_add_js('sites/all/libraries/jpageflip-0.9.7.js');
drupal_add_js('jQuery(document).ready(function () { 
       jQuery("#myPageFlip").jPageFlip({
        width: "480", 
        height: "480",
       });
  });', 'inline');
?>

what i get in the header is this:

<script type="text/javascript">
<!--//--><![CDATA[//><!--
jQuery(document).ready(function () {
jQuery("#myPageFlip").jPageFlip({
width: "480",
height: "480",
});
});
//--><!]]>
</script>

the // seem to uncomment the multiline comment start and end which was quite confusing...

Anyways, thank you again and if you have any suggestions how to debug my problem that would be great :)

Regards,
Markus

jaypan’s picture

You still haven't mentioned what the problem is.

Contact me to contract me for D7 -> D10/11 migrations.

lunk rat’s picture

Duke’s picture

I had the same issue. All Javascript code was suddenly hidden inside CDATA comments...

I fixed the problem by adding < script type="text/javascript" src="path_to_your_js_file> in the header (by editing html.tpl.php file) or right before the javascript code in the content.

The problem started after a theme update. The new html.tpl.php file erase the old one. (lost some CSS too)

Morale: Always make sub-theming or at least keep up-to-date backups.

jaypan’s picture

While CDATA isn't really necessary anymore, it shouldn't break your JavaScript. Are you sure your problem wasn't somewhere else?

Contact me to contract me for D7 -> D10/11 migrations.

lejuanchis’s picture

I have a similar problem, Drupal adds the CDATA tag to my script, however google search console send me an alert that there is an error because of that, what can I do to remove it? I already tried with the examples above but it still doesn't work.