SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast slp's Avatar
    Join Date
    May 2001
    Location
    Australia
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    referencing form element - please help!!

    My question refers to a javascript I'm working on.
    I have a form which displays its output in a textarea on the same page. This part is working well, but what I'm trying to do (without success!) is create a button which, when clicked, will open a new window which contains (only) the contents of that textarea. I should also add that the original form is in a frame.

    Is it possible to do this?! I've been looking for information on the web (even in official documentation) but nothing I've tried has worked. I'd appreciate any & all help!

  2. #2
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    This function will open a new window when you press the submit or any other button

    Code:
    function o(text) {
       var w = window.open("","");
       w.document.write("<html><body><h1>"+text+"</h1></body></html>"); 
    }
    where text is the output of the form.

    To call this function when submit button is clicked write
    Code:
     <script language=javascript>
     function myfunction() {
      ...
      o(output_of_the_form);
     }
     </script>
    
     <form onsubmit="myfunction()" ...
    or from any other button write

    Code:
     <script language=javascript>
     function myfunction() {
      ...
      o(output_of_the_form);
     }
     </script>
    
     <form ..>
     <input type=button onclick="myfunction()" ...
    Paul

  3. #3
    SitePoint Enthusiast slp's Avatar
    Join Date
    May 2001
    Location
    Australia
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    printing new window

    Thank you so much!! That works beautifully.
    My javascript knowledge is pretty poor, so I really appreciate your help!

    But now I have another problem. When I try to print that newly created window (just using the browser's print option) it prints the original window (not the new one). Very frustrating! It does this with Opera, IE and Netscape, so I'm guessing it's not a browser problem.

    Is there a Javascript-related reason it won't print the new window? Is there anything I can do??
    Last edited by slp; May 27, 2001 at 00:02.

  4. #4
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    could you include your code?

    I tested with IE 5.5 & NN 4.7 (Win) and I printed the frame's contents without problems

    Paul

  5. #5
    SitePoint Enthusiast slp's Avatar
    Join Date
    May 2001
    Location
    Australia
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suppose the relevant parts of the code would be the functions in the head section:

    Code:
    function o(text) 
    {
       var w = window.open("","");
       w.document.write("<html><body><PRE>"+text+"</PRE></body></html>");
    }
    
    function Print()
    {
     o(document.cgen.display.value);
    }
    where my form is called 'cgen' and the output textarea is called 'display'.
    And the button in the body:
    Code:
    <INPUT type="button" name ="print" value="print" onClick="Print(this.form)">
    (I've called the button & function 'print' although that's not really what it does, obviously. It really means 'printer-friendly version'.)
    And, like I said, the wrong window gets printed (using Netscape 4.04 and IE 5.0. Opera 5.01 crashes!)

  6. #6
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: printing new window

    Originally posted by slp
    When I try to print that newly created window (just using the browser's print option) it prints the original window (not the new one)
    Ok, if you want offer the visitor the option to print the window's contents just rewrite the o() function

    Code:
    function o(text) 
    {
       var w = window.open("","");
       if (window.print)
           pcommand = '<a href="javascript:parent.frames[1].print()">Click here to print this window</a><br>';
       w.document.write("<html><body>"+pcommand+"<PRE>"+text+"</PRE></body></html>");
    }
    PS This function isn't supported by IE 4.x

    Paul

  7. #7
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Note that

    parent.frames[i].print()

    where the first frame is i=0, second is i=1, etc

  8. #8
    SitePoint Enthusiast slp's Avatar
    Join Date
    May 2001
    Location
    Australia
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    printing problem solved

    Thanks for your suggestions, but when I tried it out the same thing happened - it printed out the original window.

    I was just about to give in to despair, when by an unbelievable stroke of luck, I came across someone who had the exact same problem only 2 days ago! Looks like this person finally managed to figure out the printing problem: the new window has to be closed when it's finished!

    The message can be found here: http://freewarejava.com/ubb/Forum1/HTML/016136.html

    So I've incorporated that into the code you gave me about referencing the output of the form & it all works! (Although the print() function doesn't work in some browsers, the new window is properly displayed in all, and can be successfully printed manually. So I'm happy )

    Thanks very much for all your help. I really appreciate the time you've taken, and I've learned a few things about Javascript which I'm sure will come in handy in the future!

  9. #9
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: printing problem solved

    Originally posted by slp
    Thanks very much for all your help. I really appreciate the time you've taken, and I've learned a few things about Javascript which I'm sure will come in handy in the future!
    You're welcome
    I'm glad that you have solved your problem.

    Paul


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
  •