单击刷新按钮时出现以下错误:
caught TypeError: Cannot read property 'length' of undefined
at getRandomImage (index.html:85)
at HTMLInputElement.onclick (index.html:11)
请参阅此代码段:
var myArray = new Array();
getRandomImage(myArray, "");
function getRandomImage(imgAr, path) {
path = path || 'C:/Users/Owen/Desktop/1-800MemeLine-master/memes/'; // default path here
var num = Math.floor( Math.random() * imgAr.length );
var img = imgAr[ num ];
var imgStr = '<img src="' + path + img + '" alt = "">';
document.write(imgStr); document.close();
}
<form>
<INPUT TYPE="button" onClick="getRandomImage()" VALUE="More MEME plz">
</INPUT>
</form>
按钮刷新页面工作正常,但我网站上的大多数人都有不良的互联网,需要很长时间才能重新加载页面,所以只加载图片,而不是加载背景和文字以及不必要的东西。
链接到github REPO:https://github.com/odude102/1-800MemeLine/blob/master/index.html
答案 0 :(得分:0)
这不是最佳解决方案,但它解决了您的问题:
var myArray = new Array();
var path = 'C:/Users/Owen/Desktop/1-800MemeLine-master/memes/';
getRandomImage();
function getRandomImage() {
var num = Math.floor( Math.random() * myArray.length );
var img = myArray[ num ];
var imgStr = '<img src="' + path + img + '" alt = "">';
document.write(imgStr);
document.close();
}