Site logo

Archived topic

The type attribute is unnecessary for JavaScript resources

5 replies · Started by Ciomîrtan Andrei on February 13, 2019

Viewing posts 1–6 of 6

Hi Tom,

I use validator for HTML

https://validator.w3.org/nu/?doc=https%3A%2F%2Fwww.refu.ro%2F

And the validator give me a warning:

The type attribute is unnecessary for JavaScript resources.

Where <script type="text/javascript"> is the problem for the not fully validate the blog.

This code in child functions.php help?

add_action( 'wp_enqueue_scripts', 'generatepress_child_enqueue_scripts' );

add_filter('style_loader_tag', 'myplugin_remove_type_attr', 10, 2);
add_filter('script_loader_tag', 'myplugin_remove_type_attr', 10, 2);

function myplugin_remove_type_attr($tag, $handle) {
    return preg_replace( "/type=['\"]text\/(javascript|css)['\"]/", '', $tag );
}

Thanks!

Hi there,

these are only warnings, so won't stop your site from being validated. If you must remove them, then i found this snippet:

add_action( 'template_redirect', function(){
    ob_start( function( $buffer ){
        $buffer = str_replace( array( 'type="text/javascript"', "type='text/javascript'" ), '', $buffer );        
        return $buffer;
    });
});

WordPress also natively supports HTML5 markup.

function theme_name_setup() {
    add_theme_support( 'html5', array( 'script', 'style' ) );
}
add_action( 'after_setup_theme', 'theme_name_setup' );

Thanks Elvin. That's great.

No problem. :)

This archived topic is closed to new replies.