Results 1 to 5 of 5

Thread: Help with Javascript and printing in a new window

  1. #1
    Join Date
    Dec 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Javascript and printing in a new window

    Ok I have this page (main.html) that is loaded with an iframe (that loads in this mypage2.html) that I would like to print. How I would like it to print is when a user clicks on a Print link on the main.html page that it opens a new window with this mypage2.html and then prompts the print dialog box. After the user prints mypage2.html, the new window closes.

    Well I have the following code that I added to the mypage2.html which is the page that gets loaded into the iframe:


    Code:
    <object id=WBControl width=0 height=0 classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2></object>
    <script language=VBScript><!-- //
     Sub VBPrint() On Error Resume Next
        WBControl.ExecWB 6,1
     End Sub
    //--></script>
    
    <script language=JavaScript><!-- //
    // if browser is not IE, FF or Netscape then just prints
    if (window.print) self.print();
    //Checks to see if browser is IE
    else if (navigator.appName.indexOf('Microsoft') !=-1) VBPrint()
    setTimeout('self.close()',3000);
    else if (navigator.appName.indexOf('Netscape') !=-1) VBPrint()
    setTimeout('self.close()',3000);
    //--></script>
    Then when a user clicks on the Print link back on main.html to open the mypage2.html, the code used on the link is:

    Code:
    <a href="javascript:;" onClick="window.open('mypage2.html','newwin')">Print this page</a>
    Well this works out well in IE but in FireFox and Netscape everytime the main.html page opens with the iframe loading this mypage2.html, Firefox and Netscape wants to print the parent page .

    So my question is there is someway to make Firefox and Netscape do a similar function? Or am I just way off on my code?

    Thanks for the advice.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    this isn't definite, but I would guss that FF and NS don't support your code.
    closing a window once printed is kinda vague and might not be a command supported by all browsers.
    Good luck though.

    So my question is there is someway to make Firefox and Netscape do a similar function?
    Oh... just saw that.
    Right... I think you may very well need a new code for it, if there is one... I'm not sure.


    Also, "works" means what?
    Does:
    1. the window open?
    2. the printing dialogue box pop up?
    3. it print?
    4. the window close?

    'cause... if it's just the window not closing, then who cares... FF/NS users can just close the window. If it's more than that, not sure... especially about the auto print function... but.. again, why not just let them choose print from the menu... you could setup an alert (something like-- if browser=FF/NS, alert("click print, then close window").) If you want, that is.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Please, please, please read the post?
    Well this works out well in IE but in FireFox and Netscape everytime the main.html page opens with the iframe loading this mypage2.html, Firefox and Netscape wants to print the parent page .
    Try this.
    Code:
    <object id="WBControl" width="0" height="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object>
    <script language="VBScript">
    <!--
     Sub VBPrint() On Error Resume Next
        WBControl.ExecWB 6,1
     End Sub
    ' --></script>
    
    <script type="text/javascript">
    var printfunc = (window.print || VBPrint || false);
    if(printfunc && this.name == "printwin") {
      printfunc();
      setTimeout('self.close()',3000);
    }
    
    function printThis() {
      window.open(this.location.href, "printwin");
    }
    </script>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Ah, ok. I missed a couple things... sorry... distracted.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Dec 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks Twey. The solution you provided worked out well and I can see where I went wrong with my code the first time.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •