我无法获取javascript弹出窗口(由onclick事件启动),无法在弹出窗口加载时加载多个“随机”图像。我的代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="ISO-8859-1">
<title>Popup w/ Multiple Images - Test</title>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
</head>
<html>
<body>
<head>
<script language="JavaScript">
var w = 480, h = 340;
function openWindow() {
if (document.getElementById) {
w = screen.availWidth;
h = screen.availHeight;
}
var popwidth = 800, popheight = 700;
var leftPos = (w-popwidth)/2;
var topPos = (h-popheight)/2;
msgWindow = window.open('','popup','width=' + popwidth + ',height=' +
popheight +
',top=' + topPos + ',left=' + leftPos + ', scrollbars=yes');
msgWindow.document.write
('<html><head>' +
'<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">' +
'<meta http-equiv="Content-Style-Type" content="text/css">' +
'<meta http-equiv="Content-Script-Type" content="text/javascript">' +
'<title>Popup w/ Multiple Images - Test</title>' +
'<style type="text/css">' +
'img {' +
'border:0;' +
'}' +
'</style>' +
'<script type="text/javascript">' +
'var linksNumber = 6;' +
'var myimages = [' +
'["docURL"]' +
'["docURL"]' +
'["docURL"]' +
'["docURL"]' +
'["docURL"]' +
'["docURL"]' +
'["docURL"],' +
'["docURL"],' +
'["docURL"],' +
'["docURL"],' +
'["docURL"]];' +
'var imagelinks = [' +
'["imgURL"],' +
'["imgURL "],' +
'["imgURL "],' +
'["imgURL "],' +
'["imgURL "],' +
'["imgURL "],' +
'["imgURL "],' +
'["imgURL "],' +
'["imgURL "],' +
'["imgURL "],' +
'["imgURL "],' +
'["imgURL "]];' +
'var ary0 = [];' +
'var ary1 = [];' +
'var k = 0' +
'var c, n;' +
'function randomImgDisplay() {' +
'for(c = 0; c < myimages.length; c++) {' +
'ary0[c] = c;' +
'}' +
'while(ary0.length > 0) {' +
'n = Math.floor(Math.random()*ary0.length);' +
'ary1[k] = ary0[n];' +
'k++;' +
'ary0.splice(n,1);' +
'}' +
'for(c = 0; c < linksNumber; c++) {' +
'document.getElementById("pic"+ c).src=myimages[ary1[c]];' +
'document.getElementById("lnk"+ c).href=imagelinks[ary1[c]];' +
'}' +
'}' +
'if (window.addEventListener) {' +
'window.addEventListener("load",randomImgDisplay,false);' +
'}' +
'else {' +
'if (window.attachEvent) {' +
'window.attachEvent("onload",randomImgDisplay);' +
'}' +
'}' +
'<\/script>' +
'</head><body>' +
'<div>' +
'<a id="lnk0" href="lnk0"><img id="pic0" src="img0" alt=""></a>' +
'<a id="lnk1" href="lnk1"><img id="pic1" src="img1" alt=""></a>' +
'<a id="lnk2" href="lnk2"><img id="pic2" src="img2" alt=""></a>' +
'<a id="lnk3" href="lnk3"><img id="pic3" src="img3" alt=""></a>' +
'<a id="lnk4" href="lnk4"><img id="pic4" src="img4" alt=""></a>' +
'<a id="lnk5" href="lnk5"><img id="pic5" src="img5" alt=""></a>' +
'</div>' +
'<form name="OnClickPopup">' +
'<br/>' +
'<input type="button" value="Close" onClick="window.close
();"></form></body></html>');
</script>
<form>
<input type="button" onClick="openWindow()" value="Click Me">
</form>
</body>
</html>
当我将代码分成两个javascript文件时,一个创建弹出窗口,另一个显示多个随机图像,它们各自工作正常。我正在抓紧稻草,认为可能是由于我对“ msgWindow.document.write”的使用导致了我的问题,如果可以,请提供帮助。