How to asynchronously load dynamsoft.webtwain.initate.js?

Sometimes you may find it takes too long to load the dynamsoft.webtwain.initate.js file, you can use the following code to load JS files asynchronously.

var doc = window['document'];
var temp = function (){
	
	docElement = doc && doc.documentElement,
	head = doc.getElementsByTagName('head')[0] || docElement,
	scriptOnload = doc.createElement('script').readyState ?
		function (node, callback) {
	          var oldCallback = node.onreadystatechange;
		  node.onreadystatechange = function () {
		     var rs = node.readyState;
			if (rs === 'loaded' || rs === 'complete') {
				node.onreadystatechange = null;
				oldCallback && oldCallback();
				callback.call(this);
				}
			};
		} :
		function (node, callback) {
			node.addEventListener('load', callback, false);
			node.addEventListener('error', callback, false);
		};
}

             temp.prototype.getScript = function(url, isAsync, callback) {
	        var node, u;
	        if (!(callback && typeof (callback) === 'function')) {
		 callback = function () { };
	}

	node = doc.createElement('script');

	u = ['', url].join("");
	node.src = u;

	if (isAsync)
	node.async = true;
	node.charset = 'utf-8';
	scriptOnload(node, callback);
	head.insertBefore(node, head.firstChild);
	return node;
}
          var _temp = new temp();
        _temp.getScript("https://demo.dynamsoft.com/dwt/Resources/dynamsoft.webtwain.initiate.js?t=170607",true, 
         
        function(){
	Dynamsoft.WebTwainEnv.RegisterEvent('OnWebTwainReady', Dynamsoft_OnReady); 
});

Please note, the above code is loading the dynamsoft.webtwain.initate.js from our demo as an example. You will need to change the path to load your own JS file.

Is this page helpful?