javascript JavaScript preload()函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript JavaScript preload()函数相关的知识,希望对你有一定的参考价值。

/*!
 * JavaScript preload() function – http://mths.be/preload
 * Preload images, CSS and JavaScript files without executing them
 * Script by Stoyan Stefanov – http://www.phpied.com/preload-cssjavascript-without-execution/
 * Slightly rewritten by Mathias Bynens – http://mathiasbynens.be/
 */

function preload(array) {
	var length = array.length,
	    document = window.document,
	    body = document.body,
	    isIE = 'fileSize' in document,
	    object;
	while (length--) {
		if (isIE) {
			new Image().src = array[length];
			continue;
		}
		object = document.createElement('object');
		object.data = array[length];
		object.width = object.height = 0;
		body.appendChild(object);
	}
}

// Example:

preload([
	'http://hang.nodester.com/foo.png?2000',
	'http://hang.nodester.com/foo.js?2000',
	'http://hang.nodester.com/foo.css?2000'
]);
/*!
 * $.preload() function for jQuery – http://mths.be/preload
 * Preload images, CSS and JavaScript files without executing them
 * Script by Stoyan Stefanov – http://www.phpied.com/preload-cssjavascript-without-execution/
 * Slightly rewritten by Mathias Bynens – http://mathiasbynens.be/
 * Note that since this script relies on jQuery, the preloading process will not start until jQuery has finished loading.
 */

jQuery.preload = function(array) {
	var length = array.length,
	    document = window.document,
	    body = document.body,
	    isIE = 'fileSize' in document,
	    object;
	while (length--) {
		if (isIE) {
			new Image().src = array[length];
			continue;
		}
		object = document.createElement('object');
		object.data = array[length];
		object.width = object.height = 0;
		body.appendChild(object);
	}
};

// Example:

$(function() {
	$.preload([
		'http://hang.nodester.com/foo.png?2000',
		'http://hang.nodester.com/foo.js?2000',
		'http://hang.nodester.com/foo.css?2000'
	]);
});

以上是关于javascript JavaScript preload()函数的主要内容,如果未能解决你的问题,请参考以下文章

[TimLinux] JavaScript 代码控制滚动条移动到顶部/底部

This seems to be a pre-built javascript file. webpack报这个警告怎么办?

《剑指offer》— JavaScript重建二叉树

JavaScript之正则匹配索引RegExpmatchAllreduceconcattoStringsplitincludesString/d

为什么解构与Javascript(ES6)中的经典赋值不同?

JavaScript实现扁平数组结构与JSON树形结构相互转换递归reducecontinuepushconcatfor of