Remove ‘type’ attribute in Drupal 7 – Say goodbye style, script & link!

Remove 'type' attribute in Drupal 7 - Say goodbye style, script & link!

Here’s a quick tip to remove the type attribute in Drupal 7 to make your site more HTML5 code compliant. This along with other HTML5 features allows for less code on the front-end… and who doesn’t like less code?!

No longer do you need to include this attribute in your script, style and link tags. As an added bonus, this Drupal hook will also remove CDATA comments. They only existed to satisfy the XHTML validator and it too has gone away with the release of HTML5.

Remove type attribute Drupal 7: function theme_process_html_tag

See http://api.drupal.org/api/drupal/includes!theme.inc/function/theme_html_tag/7 for more details.

function bmarshall_process_html_tag(&$vars) {
  $el = &$vars['element'];

  // Remove type="..." and CDATA prefix/suffix.
  unset($el['#attributes']['type'], $el['#value_prefix'], $el['#value_suffix']);

  // Remove media="all" but leave others unaffected.
  if (isset($el['#attributes']['media']) && $el['#attributes']['media'] === 'all') {
    unset($el['#attributes']['media']);
  }
}

Check out the comparison below of the before and after using a slimmed down result.

XHTML Before: with the type attribute

<link rel="stylesheet" href="..." type="text/css" media="all">

<style type="text/css" media="all">
/* CSS code here. */
</style>

<script type="text/javascript">
<!--//--><![CDATA[//><!--
/* JavaScript code here. */
//--><!]]>
</script>

HTML5 After: without the type attribute

<link rel="stylesheet" href="...">

<style>
/* CSS code here. */
</style>

<script>
/* JavaScript Code here. */
</script>

Author: Ben Marshall

Red Bull Addict, Self-Proclaimed Grill Master, Entrepreneur, Workaholic, Front End Engineer, SEO/SM Strategist, Web Developer, Blogger

Leave a Reply

Your email address will not be published. Required fields are marked *