5

On my site i am getting some unnecessary CDATA script on the page source.

    <script type="text/javascript">
<!--//--><![CDATA[//><!--
jQuery.extend(Drupal.settings, {"basePath":"\/","pathPrefix":"","ajaxPageState":{"theme":"visapro","theme_token":"IoYFb0sv8oueNJZfQl2VKQDzEs_MwLTh97vN5AXTVQU","js":{"misc\/jquery.js":1,"misc\/jquery.once.js":1,"misc\/drupal.js":1,"misc\/ui\/jquery.ui.core.min.js":1,"misc\/jquery.ba-bbq.js":1,"modules\/overlay\/overlay-parent.js":1,"sites\/all\/libraries\/shadowbox\/shadowbox.js":1,"sites\/all\/modules\/shadowbox\/shadowbox_auto.js":1,"modules\/contextual\/contextual.js":1,"misc\/jquery.cookie.js":1,"sites\/all\/modules\/panels\/js\/panels.js":1,"modules\/toolbar\/toolbar.js":1,"sites\/all\/themes\/visapro\/js\/homeslider.js":1,"sites\/all\/themes\/visapro\/js\/jquery.bxslider.min.js":1,"sites\/all\/themes\/visapro\/js\/jquery.anythingslider.js":1,"sites\/all\/themes\/visapro\/js\/qswfobject.js":1,"sites\/all\/themes\/visapro\/js\/jquery.colorbox.js":1,"sites\/all\/themes\/visapro\/js\/prettify.js":1,"sites\/all\/themes\/visapro\/js\/sliders.js":1,"0":1},"css":{"modules\/system\/system.base.css":1,"modules\/system\/system.menus.css":1,"modules\/system\/system.messages.css":1,"modules\/system\/system.theme.css":1,"misc\/ui\/jquery.ui.core.css":1,"misc\/ui\/jquery.ui.theme.css":1,"modules\/overlay\/overlay-parent.css":1,"sites\/all\/libraries\/shadowbox\/shadowbox.css":1,"0":1,"modules\/contextual\/contextual.css":1,"sites\/all\/modules\/simplenews\/simplenews.css":1,"modules\/comment\/comment.css":1,"modules\/field\/theme\/field.css":1,"modules\/node\/node.css":1,"modules\/search\/search.css":1,"modules\/user\/user.css":1,"sites\/all\/modules\/views\/css\/views.css":1,"sites\/all\/modules\/better_messages\/better_messages_admin.css":1,"sites\/all\/modules\/ctools\/css\/ctools.css":1,"sites\/all\/modules\/panels\/css\/panels.css":1,"modules\/shortcut\/shortcut.css":1,"modules\/toolbar\/toolbar.css":1,"sites\/all\/themes\/visapro\/css\/layout.css":1,"sites\/all\/themes\/visapro\/css\/style.css":1,"sites\/all\/themes\/visapro\/css\/colors.css":1,"sites\/all\/themes\/visapro\/css\/view-home.css":1,"sites\/all\/themes\/visapro\/css\/theme-metallic.css":1,"sites\/all\/themes\/visapro\/css\/jquery.bxslider.css":1,"sites\/all\/themes\/visapro\/css\/anythingslider.css":1,"sites\/all\/themes\/visapro\/css\/colorbox.css":1,"sites\/all\/themes\/visapro\/css\/print.css":1}},"overlay":{"paths":{"admin":"media\/*\/edit\nmedia\/*\/multiedit\nmedia\/*\/delete\nmedia\/browser\nmedia\/browser\/*\nnode\/*\/edit\nnode\/*\/delete\nnode\/*\/revisions\nnode\/*\/revisions\/*\/revert\nnode\/*\/revisions\/*\/delete\nnode\/add\nnode\/add\/*\noverlay\/dismiss-message\nuser\/*\/shortcuts\nnode\/*\/simplenews\nadmin\nadmin\/*\nbatch\ntaxonomy\/term\/*\/edit\nuser\/*\/cancel\nuser\/*\/edit\nuser\/*\/edit\/*","non_admin":"admin\/structure\/block\/demo\/*\nadmin\/reports\/status\/php"},"pathPrefixes":[],"ajaxCallback":"overlay-ajax"},"shadowbox":{"animate":true,"animateFade":true,"animSequence":"wh","auto_enable_all_images":0,"auto_gallery":0,"autoplayMovies":true,"continuous":false,"counterLimit":10,"counterType":"default","displayCounter":true,"displayNav":true,"enableKeys":true,"fadeDuration":0.35,"handleOversize":"resize","handleUnsupported":"link","initialHeight":160,"initialWidth":320,"language":"en","modal":false,"overlayColor":"#000","overlayOpacity":0.8,"resizeDuration":0.55,"showMovieControls":true,"slideshowDelay":0,"viewportPadding":20,"useSizzle":false},"tableHeaderOffset":"Drupal.toolbar.height"});
//--><!]]>
</script>

I want to remove it. I am unable to find the exact source form where it is come. Please suggest me with a solution.

  • You want to remove cdata only, as Clive appears to think, or the whole script, as AyeshK apparently assumed in his answer? Please clarify. – Mołot Jun 19 '13 at 12:22
3

I'm assuming you're using an HTML5 DOCTYPE, The CDATA tags are required for XML-based documents (including XHTML).

The characters look to be hard-coded into common.inc, line 4287:

$embed_prefix = "\n<!--//--><![CDATA[//><!--\n";
$embed_suffix = "\n//--><!]]>\n";

Examining that code I think the only way to intercept it would be to override theme_html_tag() in your theme, e.g.

function MYTHEME_html_tag($vars) {
  if ($vars['element']['#tag'] == 'script') {
    unset($vars['element']['#value_prefix'];
    unset($vars['element']['#value_suffix'];
  }

  return theme_html_tag($vars);
}
1

For Drupal 7, where every theme function has preprocess functions, you could use the following code.

function mytheme_preprocess_html_tag(&$variables) {
  if ($variables['element']['#tag'] == 'script') {
    if (strpos($variables['element']['#value'], "\n<!--//--><![CDATA[//><!--\n") !== FALSE) {
      $variables['element']['#value_prefix'] = str_replace("\n<!--//--><![CDATA[//><!--\n", "", $variables['element']['#value_prefix']);
    }
    if (strpos($variables['element']['#value'], "\n//--><!]]>\n") !== FALSE) {
      $variables['element']['#value_suffix'] = str_replace("\n//--><!]]>\n", "", $variables['element']['#value_suffix']);
    }
  }
}

I remove the prefix and suffix only when they are present; I also remove only the part we don't want, not the full prefix/suffix.

0

Not sure this is what you were asking (remove the CDATA tags), but I came across this post in my search of wanting to remove the CDATA tags for one inline JS script, and Clive's answer got me headed in the right direction. But I ultimately used...

function MYTHEME_preprocess_html_tag(&$vars) {
  if (strpos($vars['element']['#value'], $needle) !== FALSE) {
    unset($vars['element']['#value_prefix']);
    unset($vars['element']['#value_suffix']);
  }
}

Just to give another example if anyone comes across the same scenario.

0

Set the field as: Text format "PHP" then put your css clases inside the tag style

  • You should explain how this is going to help the OP. – kiamlaluno Jul 17 '14 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy