Search code examples
javascriptjavajqueryfirefoxapplet

JS not working properly in firefox


I am using qzprint API for printing labels in my open cart extension. Everything was working fine but suddenly it stopped working on FF. In Internet explorer it works fine. If i add alerts in my functions of applet it works fine on firefox as well but not sure why not with out alerts. here is my code.

calling applet functions in my header.tpl

<script type="text/javascript">
deployQZ('<?php echo HTTP_CATALOG ?>');
useDefaultPrinter();
<script>

Applet file containing functions

function deployQZ(path) {
//alert("alert for printing label");
    pathApplet = path + 'java/qz-print.jar';
    pathJnlp = path + 'java/qz-print_jnlp.jnlp';
    var attributes = {id: "qz", code:'qz.PrintApplet.class', 
        archive: pathApplet, width:1, height:1};
    var parameters = {jnlp_href: pathJnlp, 
        cache_option:'plugin', disable_logging:'false', 
        initial_focus:'false'};
    if (deployJava.versionCheck("1.7+") == true) {}
    else if (deployJava.versionCheck("1.6+") == true) {
        attributes['archive'] = 'java/jre6/qz-print.jar';
        parameters['jnlp_href'] = 'java/jre6/qz-print_jnlp.jnlp';
    }
    deployJava.runApplet(attributes, parameters, '1.5');
}

/**
* Automatically gets called when applet has loaded.
*/
function qzReady() {
    // Setup our global qz object
    window["qz"] = document.getElementById('qz');
    //var title = document.getElementById("title");
    if (qz) {
        try {
            //title.innerHTML = title.innerHTML + " " + qz.getVersion();
            //document.getElementById("content").style.background = "#F0F0F0";
        } catch(err) { // LiveConnect error, display a detailed meesage
            document.getElementById("content").style.background = "#F5A9A9";
            alert("ERROR:  \nThe applet did not load correctly.  Communication to the " + 
                "applet has failed, likely caused by Java Security Settings.  \n\n" + 
                "CAUSE:  \nJava 7 update 25 and higher block LiveConnect calls " + 
                "once Oracle has marked that version as outdated, which " + 
                "is likely the cause.  \n\nSOLUTION:  \n  1. Update Java to the latest " + 
                "Java version \n          (or)\n  2. Lower the security " + 
                "settings from the Java Control Panel.");
      }
  }
}

    /**
* Returns is the applet is not loaded properly
*/
function isLoaded() {
    if (!qz) {
        alert('Error:\n\n\tPrint plugin is NOT loaded!');
        return false;
    } else {
        try {
            if (!qz.isActive()) {
                alert('Error:\n\n\tPrint plugin is loaded but NOT active!');
                return false;
            }
        } catch (err) {
            alert('Error:\n\n\tPrint plugin is NOT loaded properly!');
            return false;
        }
    }
    return true;
}

    function useDefaultPrinter() {
    //alert("alert for printing label");
    if (isLoaded()) {
        // Searches for default printer
        qz.findPrinter();

        // Automatically gets called when "qz.findPrinter()" is finished.
        window['qzDoneFinding'] = function() {
            // Alert the printer name to user
            var printer = qz.getPrinter();
            //alert(printer !== null ? 'Default printer found: "' + printer + '"':
                //'Default printer ' + 'not found');
            document.getElementById("name_printer").innerHTML = 'Default printer found: "' + printer + '"'; 
            // Remove reference to this function
            window['qzDoneFinding'] = null;
            defaultFound = true;
        };
    }
}

As u can see in my deployqz() and usedefaultprinter() functions i have alert on first line which is in comments if its commented it doesn't work in fire fox and if not commented than it works fine. With comments i get alert message from isLoaded() function "Print plugin is NOT loaded properly!".

Also in my console i get this

An unbalanced tree was written using document.write() causing data from the network to be reparsed. For more information https://developer.mozilla.org/en/Optimizing_Your_Pages_for_Speculative_Parsing


Solution

  • Try this:

    1. If the qzReady is called by the applet when ready, put useDefaultPrinter inside that function.
    2. if isLoaded takes some time, call useDefaultPrinter in there too using setTimeout

    Like this

    <script type="text/javascript">
    deployQZ('<?php echo HTTP_CATALOG ?>');
    <script>
    

    Applet file containing functions

    var qz;
    
    function deployQZ(path) {
        pathApplet = path + 'java/qz-print.jar';
        pathJnlp = path + 'java/qz-print_jnlp.jnlp';
        var attributes = {id: "qz", code:'qz.PrintApplet.class', 
            archive: pathApplet, width:1, height:1};
        var parameters = {jnlp_href: pathJnlp, 
            cache_option:'plugin', disable_logging:'false', 
            initial_focus:'false'};
        if (deployJava.versionCheck("1.7+") == true) {}
        else if (deployJava.versionCheck("1.6+") == true) {
            attributes['archive'] = 'java/jre6/qz-print.jar';
            parameters['jnlp_href'] = 'java/jre6/qz-print_jnlp.jnlp';
        }
        deployJava.runApplet(attributes, parameters, '1.5');
    }
    
    /**
    * Automatically gets called when applet has loaded.
    */
    function qzReady() {
        // Setup our global qz object
        qz = document.getElementById('qz');
        if (qz) {
            try {
              useDefaultPrinter();
            } catch(err) { // LiveConnect error, display a detailed meesage
                document.getElementById("content").style.background = "#F5A9A9";
                alert("ERROR:  \nThe applet did not load correctly.  Communication to the " + 
                    "applet has failed, likely caused by Java Security Settings.  \n\n" + 
                    "CAUSE:  \nJava 7 update 25 and higher block LiveConnect calls " + 
                    "once Oracle has marked that version as outdated, which " + 
                    "is likely the cause.  \n\nSOLUTION:  \n  1. Update Java to the latest " + 
                    "Java version \n          (or)\n  2. Lower the security " + 
                    "settings from the Java Control Panel.");
          }
       }
       else { setTimeout(useDefaultPrinter,300); }
    }
    
        /**
    * Returns is the applet is not loaded properly
    */
    function isLoaded() {
        if (!qz) {
            alert('Error:\n\n\tPrint plugin is NOT loaded!');
            return false;
        } else {
            try {
                if (!qz.isActive()) {
                    alert('Error:\n\n\tPrint plugin is loaded but NOT active!');
                    return false;
                }
            } catch (err) {
                alert('Error:\n\n\tPrint plugin is NOT loaded properly!');
                return false;
            }
        }
        return true;
    }
    
    function useDefaultPrinter() {
        //alert("alert for printing label");
        if (isLoaded()) {
            // Searches for default printer
            qz.findPrinter();
    
            // Automatically gets called when "qz.findPrinter()" is finished.
            window['qzDoneFinding'] = function() {
                // Alert the printer name to user
                var printer = qz.getPrinter();
                //alert(printer !== null ? 'Default printer found: "' + printer + '"':
                    //'Default printer ' + 'not found');
                document.getElementById("name_printer").innerHTML = 'Default printer found: "' + printer + '"'; 
                // Remove reference to this function
                window['qzDoneFinding'] = null;
                defaultFound = true;
            };
        }
        else { setTimeout(useDefaultPrinter,300); }
    }