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

Written by
Published
Updated
Typical Read
2 minutes

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.

[php]
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’]);
}
}
[/php]

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

XHTML Before: with the type attribute

[html]
<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>
[/html]

HTML5 After: without the type attribute

[html]
<link rel="stylesheet" href="…">

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

<script>
/* JavaScript Code here. */
</script>
[/html]

1 comment on “Remove ‘type’ attribute in Drupal 7 – Say goodbye style, script & link!”.

# Dec 16, 2017

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.

Join the conversation.

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

All comments posted on 'Remove ‘type’ attribute in Drupal 7 – Say goodbye style, script & link!' are held for moderation and only published when on topic and not rude. Get a gold star if you actually read & follow these rules.

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.