我无法在页面中打开具有相同 url 的多个窗口,每次在 for 循环中调用 window.open 时,旧的迷你窗口的内容都会被新内容替换。但我不想替换内容而是单独打开一个新窗口。
JavaScript 代码:
function OpenMultipleWindows() {
var winContent = ['Page1', 'Page2', 'Page3', 'Page4']
for (var i = 0; i < winContent.length; i++) {
(function (text) {
var newWin = open('/localFolder/window.html', 'Print', 'width=500,height=500', '_blank');
newWin.document.body.innerHTML = winContent[text];
}(i));
}
}
$(function () {
OpenMultipleWindows();
});
和window.html
html代码:-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<script type="text/VBScript">
document.write "<object ID='WB' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>"
Sub printitinvb()
On Error Resume Next
OLECMDID_PRINT = 6
OLECMDEXECOPT_DONTPROMPTUSER = 2
OLECMDEXECOPT_PROMPTUSER = 1
call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, 1)
End Sub
</script>
<script type="text/javascript">
function printit() {
printitinvb();
}
</script>
</head>
<body onload='printit();'>
</body>
</html>
这个问题有解决办法吗?
最佳答案
您的问题是,您没有更改弹出窗口的标识符。 你可以尝试这样的事情:
function OpenMultipleWindows() {
var winContent = ['Page1', 'Page2', 'Page3', 'Page4'];
for (var i = 0; i < winContent.length; i++) {
(function (text) {
var newWin = open('/localFolder/window.html', 'Print' + i, 'width=500,height=500', '_blank');
newWin.document.body.innerHTML = winContent[text];
}(i));
}
}
$(function () {
OpenMultipleWindows();
});
第二个参数是标识符,必须是唯一的。
//编辑:添加注释作为评论的答案
您需要等待文档在弹出窗口中加载。我通常为此添加一个 JavaScript 元素。但你可以像我认为的那样(未经测试):
function addTextToPopup(handle, text){
if (!handle|| handle.closed) return;
var bodyElem = handle.document.getElementsByTagName('body');
if (!bodyElem || bodyElem.length !== 1){
//check again after a short period
window.setTimeout(function(){
addTextToPopup(handle, text);
}, 250);
return;
}
handle.document.body.innerHTML = winContent[text];
}
function OpenMultipleWindows() {
var winContent = ['Page1', 'Page2', 'Page3', 'Page4'];
for (var i = 0; i < winContent.length; i++) {
(function (text) {
var newWin = open('/localFolder/window.html', 'Print' + i, 'width=500,height=500', '_blank');
addTextToPopup(newWin, winContent[text]);
}(i));
}
}
$(function () {
OpenMultipleWindows();
});
如果这不起作用,请尝试附加一个 JS 变量,例如 domIsReady = false;
并在弹出的 html onload 事件上将其设置为 true,并使用 newWin 检查这是否为 true。 domIsReady === true
关于javascript - 使用相同的 URL、JavaScript 打开多个迷你窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23903914/