Flaky IFrame Print Issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mugs321
    New Member
    • Sep 2006
    • 22

    Flaky IFrame Print Issue

    Hey all,

    Here's the deal... I have an iframe which I am using a to print a certain page without displaying said page. I've use this technique in other areas of my site and they work like a charm. Using the same code as all the other pages:

    From page with print icon:
    Code:
    function printStudents(url1){
        objFrame = document.getElementById('printFrame');
        objFrame.src = url1;
        objFrame.focus();
    }
    From page (loaded in iframe) to be printed:
    Code:
    function doPrint(){
        this.focus();
        this.print();
    }
    document.onload=doPrint();
    The iframe will not print when the icon is pressed though the function is being run (confirmed with an 'alert()'). HOWEVER, if I press my browser's 'Back' button then return to the page using 'Forward', the print dialog displays. It will also print if I load the iframe page in my browser normally.

    Any thoughts?
  • Mugs321
    New Member
    • Sep 2006
    • 22

    #2
    Figured it out... guess it was a timing issue.

    I added
    Code:
    document.onload=setTimeout("doPrint()", 100);
    to the iframe page and it works fine now.

    Comment

    • Dasty
      Recognized Expert New Member
      • Nov 2007
      • 101

      #3
      Originally posted by Mugs321
      Figured it out... guess it was a timing issue.

      I added
      Code:
      document.onload=setTimeout("doPrint()", 100);
      to the iframe page and it works fine now.
      No, it is not ... it something different.

      Code:
      function doPrint(){
          this.focus();
          this.print();
      }
      document.onload=doPrint();
      Look at your code closely. by doPrint(); you are calling doPrint function and you are storing result of this function into document.onload . So, you are not calling doPrint function after document is loaded as you would expect (which may cause issues as you described).

      fix it that way:

      Code:
      function doPrint(){
          this.focus();
          this.print();
      }
      window.onload=doPrint;
      Here, we are storing function reference into onload event as event handler which is now correctly fired after document is loaded. It's correct to use window.onload.

      I would suggest to try this new code, because it's always risky to use timeout functions. It may work on your fast computer, but what if 100ms will be not enough for some users?

      Comment

      • Mugs321
        New Member
        • Sep 2006
        • 22

        #4
        Dasty,
        Your suggestion worked great. I am curious, however, as to why the script only executes properly when using window.onload (as opposed to document.onload )

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Originally posted by Mugs321
          Dasty,
          Your suggestion worked great. I am curious, however, as to why the script only executes properly when using window.onload (as opposed to document.onload )
          Use onload with the window object - see link.

          Comment

          Working...