弹出错误中没有定义变量,但我已经定义了它

时间:2014-03-25 23:16:40

标签: javascript html css

这里有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's 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函数调用(使用PHP生成img标记,因此忽略反斜杠):

<img onLoad=\"c++;count_loaded();\"

其次,下面的代码会写一个div,它将填充图像,但我不想让它们渲染(我已经尝试将div的显示设置为无,但它们仍然存在加载到内存中...我尝试将注释标记放在div周围,因为我需要的只是获取它的innerHTML。

但是当我使用JavaScript获取innerHTML并将其加载到弹出窗口时它没有显示,它不应该仍然显示在弹出窗口中,因为我得到了div的innerHTML (图像)并将它们放入div而没有围绕它的标签。

echo " <div id=\"print_container2\" style=\"display:none;\">".$hires."</div> ";

########### EDIT ############ 更新了我的代码,感谢Joe。

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);

但弹出窗口没有得到&#34; div&#34;附加到它,这是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>

1 个答案:

答案 0 :(得分:3)

您的整个方法都存在问题。让我们尝试完全不同的东西吧。创建一个新文件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);
};

然后创建一个带有html的generating_images.html页面。 调用上面的函数将变量和函数传递给弹出窗口。