2022-01-06 12:53:30 +00:00
|
|
|
/**
|
|
|
|
* Adds a script to the page if it has not already been loaded. JS version of `wp_enqueue_script`.
|
|
|
|
*
|
2022-03-18 11:45:14 +00:00
|
|
|
* @param {Object} script WP_Script
|
2022-01-06 12:53:30 +00:00
|
|
|
* @param {string} script.handle Script handle.
|
2022-03-18 11:45:14 +00:00
|
|
|
* @param {string} script.src Script URL.
|
2022-01-06 12:53:30 +00:00
|
|
|
*/
|
|
|
|
export function enqueueScript( script ) {
|
|
|
|
return new Promise( ( resolve, reject ) => {
|
|
|
|
if ( document.querySelector( `#${ script.handle }-js` ) ) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
const domElement = document.createElement( 'script' );
|
|
|
|
domElement.src = script.src;
|
|
|
|
domElement.id = `${ script.handle }-js`;
|
|
|
|
domElement.async = true;
|
|
|
|
domElement.onload = resolve;
|
|
|
|
domElement.onerror = reject;
|
|
|
|
document.body.appendChild( domElement );
|
|
|
|
} );
|
|
|
|
}
|