Martyn Ball
Martyn Ball

Reputation: 4895

variable is not defined in popup error, but I have defined it

2 questions here really, firstly I have got the following code, my JavaScript debugger is saying that the variable "c" has not been defined when a function tries to call it, but it IS defined...

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

Function Call:

<span onClick="print_sales('print_container')" class="btgreen">Print Products</span>

count_loaded function call (the img tag is generated with PHP so ignore the backslash):

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

Secondly, the code below will write a div and it will be populated with images, but I don't want these to render (I have tried setting the div's display to none but they are still being loaded into memory... I tried putting the comment tags around the div as all I need it for is to get it's innerHTML.

But when I get the innerHTML using JavaScript and load it into a popup it doesn't display, shouldn't it still display in the popup as i'm getting the div's innerHTML (images) and putting them into a div without the tags around it.

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

###########EDIT############ Updated my code to this, thanks 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);

But the popup doesn't get the "div" appended to it, here is the generate_print.php file:

<!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>

Upvotes: 0

Views: 1546

Answers (1)

000
000

Reputation: 27247

Your entire approach is in question. Let's try something different entirely. Create a new file, generating_images.html, and move as much of the static stuff as you can.

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>

Then, you can control this popup from the containing window:

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

Then create a generating_images.html page with the html stubbed out. Call this function above to pass the variables and function into the popup.

Upvotes: 3

Related Questions