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

1 Comment

Join the Conversation

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>

Comments

  1. at 3:31 am

    Hi, great thing, help me a lot, but only works with css styles, with script still not working, or i can not make it work.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

All comments are held for moderation. I'll publish all comments that are on topic and not rude. You'll even get little stars if you do an extra good job.

You may write comments in Markdown. This is the best way to post any code, inline like `<div>this</div>` or multiline blocks within triple backtick fences (```) with double new lines before and after.

Want to tell me something privately, like pointing out a typo or stuff like that? Contact Me.

%d bloggers like this: