php 将async或defer属性添加到排队的脚本中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 将async或defer属性添加到排队的脚本中相关的知识,希望对你有一定的参考价值。

/**
 * Adds 'async' or 'defer' attributes to certain script elements.
 *
 * @param string $tag    The base HTML markup
 * @param string $handle The script's handle (first parameter in wp_enqueue_script)
 * @param string $src    The script's source (second parameter in wp_enqueue_script)
 *
 * @return string        The complete HTML markup for the script tag.
 */
function pixel_designs_script_special_attributes ( $tag, $handle, $src ) {
	// For scripts that require the 'async' attribute, include their handles in this array.
	$async = [
		'google-maps-js',
	];
	
	// For scripts that require the 'defer' attribute, include their handles in this array.
	$defer = [
		'google-maps-js',
	];
	
	if ( in_array( $handle, $async ) ) {
		$tag = str_replace( 'src', 'async src', $tag );
	}
	if ( in_array( $handle, $defer ) ) {
		$tag = str_replace( 'src', 'defer src', $tag );
	}
	
	return $tag;
}
add_filter( 'script_loader_tag', 'pixel_designs_script_special_attributes', 10, 3 );

以上是关于php 将async或defer属性添加到排队的脚本中的主要内容,如果未能解决你的问题,请参考以下文章

理解JS中script标签defer和async属性

script标签中defer和async属性的区别

defer=“defer”和async=“async”

defer和async的区别

defer 和 async 区别

js原理:async 和 defer 属性的区别