2 вопроса здесь, во-первых, у меня есть следующий код, мой отладчик JavaScript говорит, что переменная "c" не была определена, когда функция пытается ее вызвать, но она определена...
function print_sales(container) {
var pconfirm = window.confirm("Confirm you are ready to print?");
if (pconfirm == true) {
var popup = window.open('','print_products','toolbar=0,stat=0');
var div = popup.document.createElement('div');
div.setAttribute('class','print_container');
div.setAttribute('id','print_container');
div.setAttribute('style','display:none;');
div.innerHTML = doc('print_container2').innerHTML;
popup.document.write(
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'+
'<html><head>'+
'<script type="text/javascript">'+
'var i = '+doc('print_container2').getElementsByTagName('img').length+'; var c = 0;'+
'function count_loaded() {'+
'if (c == i) {'+
'document.getElementById("print_container").style.display = "block";document.getElementById("loading").style.display = "none";'+
'focus();'+
'} else {'+ //print();close();
'var p = c * 100 / i;'+
'document.getElementById("loaded").style.width = "20%"'+
'document.getElementById("loaded").innerHTML = c + "/" + i + " images generated..."'+
'}'+
'}'+
'</script>'+
' <link rel="stylesheet" href="css/css.css">'+
'</head><body><h1 id="loading">Generating images, please wait...</h1><span id="loaded"></span>'+
'</body></html>'
);
//Add images
popup.document.body.appendChild(div);
//Now check to see if any images where set to cancel, send the rest
//to be set to printed in the database
var container = doc(container);
var imgs = container.getElementsByTagName('img');
var printed = new Array();
//Create a list of id which have been printed
for(var i=0;i < imgs.length;i++) {
var id = imgs[i].id.split("_");
id = id[0]+"_"+id[1];
if (!hasClass(imgs[i], 'print_hide')) {
printed[i] = imgs[i].id.split("_")[1];
}
}
//Send printed sales to change in database
//window.location = "php/process_printed.php?printed="+cleanArray(printed).toString();
}
}
Вызов функции:
<span onClick="print_sales('print_container')" class="btgreen">Print Products</span>
count_loaded вызов функции (тэг img генерируется с помощью PHP, поэтому игнорируйте обратную косую черту):
<img onLoad=\"c++;count_loaded();\"
Во-вторых, код ниже будет писать div, и он будет заполнен изображениями, но я не хочу, чтобы они отображались (я попытался настроить отображение div на none, но они все еще загружаются в память... Я попытался поставить теги комментариев вокруг div, поскольку все, в чем я нуждаюсь, это получить его innerHTML.
Но когда я получаю innerHTML с помощью JavaScript и загружаю его в всплывающее окно, оно не отображается, оно не должно отображаться во всплывающем окне, так как я получаю div innerHTML (изображения) и помещаю их в div без тегов вокруг Это.
echo " <div id=\"print_container2\" style=\"display:none;\">".$hires."</div> ";
########### EDIT ############ Обновленный мой код, спасибо Джо.
var popup = window.open('includes/generate_print.php','print_products','toolbar=0,stat=0');
var div = popup.document.createElement('div');
div.setAttribute('class','print_container');
div.setAttribute('id','print_container');
div.setAttribute('style','display:none;');
div.innerHTML = doc('print_container2').innerHTML;
popup.window.i = doc('print_container2').getElementsByTagName('img').length;
popup.window.c = 0;
popup.window.count_loaded = function() {
if (c == i) {
document.getElementById("print_container").style.display = "block";
document.getElementById("loading").style.display = "none";
focus();
} else {
var p = c * 100 / i;
document.getElementById("loaded").style.width = p+"%";
document.getElementById("loaded").innerHTML = c + "/" + i + " images generated...";
}
};
//Add images
popup.document.body.appendChild(div);
Но всплывающее окно не получает добавленный к нему "div", вот файл generate_print.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Send to Printer</title>
</head>
<body>
<input type="button" onclick="(typeof c != 'undefined')?alert('Yes'):alert('No');" value="Is C defined?"/>
</body>
</html>
Весь ваш подход под вопросом. Попробуй что-нибудь другое. Создайте новый файл, generating_images.html
, и переместите как можно больше статического материала.
generating_images.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<script type="text/javascript">
function count_loaded(i, c) {
if (c == i) {
document.getElementById("print_container").style.display = "block";
document.getElementById("loading").style.display = "none";
focus();
} else {
var p = c * 100 / i;
document.getElementById("loaded").style.width = "20%";
document.getElementById("loaded").innerHTML = c + "/" + i + " images generated...";
}
}
</script>
<link rel="stylesheet" href="css/css.css">
</head><body><h1 id="loading">Generating images, please wait...</h1><span id="loaded"></span>
<div id="print_container" class="print_container" style="display:none"></div>
</body></html>
Затем вы можете управлять этим всплывающим окном из содержащего окна:
var popup = window.open('generating_images.html','print_products','toolbar=0,stat=0');
popup.window.onload = function() {
popup.document.getElementById('print_container').innerHTML = doc('print_container2').innerHTML;
var i = doc('print_container2').getElementsByTagName('img').length;
var c = 0;
popup.window.count_loaded(i, c);
};
Затем создайте generating_images.html
страницу с HTML затушил. Вызовите эту функцию выше, чтобы передать переменные и функцию во всплывающее окно.