# wp-optimize/4.5.4/js/loadAsync.js

WP-Optimize – Cache, Compress images, Minify &amp; Clean database to boost page speed &amp; performance, version 4.5.4. 23 lines.

- Page: https://pluginprobe.com/plugins/wp-optimize/4.5.4/code/js/loadAsync.js
- Raw: https://pluginprobe.com/plugins/wp-optimize/4.5.4/raw/js/loadAsync.js
- Modified: 2020-03-27T15:42:24+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/wp-optimize/4.5.4/code/js/loadAsync.js#L10-L20`.

```javascript
/**
 * This function will work cross-browser for loading scripts asynchronously
 */
function loadAsync(src, callback) {
	var scriptTag,
		ready = false;

	scriptTag = document.createElement('script');
	scriptTag.type = 'text/javascript';
	scriptTag.src = src;
	scriptTag.onreadystatechange = function() {
		// console.log( this.readyState ); //uncomment this line to see which ready states are called.
		if (!ready
			&& (!this.readyState || this.readyState == 'complete')
		) {
			ready = true;
			typeof callback === 'function' && callback();
		}
	};
	scriptTag.onload = scriptTag.onreadystatechange
	
	document.getElementsByTagName("head")[0].appendChild(scriptTag)
}
```
