Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: page breaks

  1. #1
    New Coder
    Join Date
    Apr 2004
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    page breaks

    What JavaScript can I use for page breaks?

    Is there a good JavaScript for clicking a button and sending a page to print?

    Thanks.

    RO

  • #2
    New Coder
    Join Date
    Mar 2004
    Location
    UK
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not sure about page breaks, but yes you can print using javascript, I found something in the past at www.cgiscript.net, although as I couldn't find it again there when I looked to day to give you the link, I've reproduced it here.


    This Script allows users to print the current html page.

    //1. INSERT THE FOLLOWING IN THE HEAD SECTION

    <script language="Javascript1.2">
    <!--
    // ***********************************************
    // AUTHOR: WWW.CGISCRIPT.NET, LLC
    // URL: http://www.cgiscript.net
    // Use the script, just leave this message intact.
    // Download your FREE CGI/Perl Scripts today!
    // ( http://www.cgiscript.net/scripts.htm )
    // ***********************************************

    function printWindow(){
    browserVersion = parseInt(navigator.appVersion)
    if (browserVersion >= 4) window.print()
    }

    // -->
    </script>


    //2. INSERT THIS IS THE BODY SECTION

    <a href="javascript: printWindow()">Print This Page</a>
    Last edited by bherrington; 06-04-2004 at 05:57 PM.
    BRUCE

  • #3
    New Coder
    Join Date
    Mar 2004
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    page breaks can be controlled by CSS properties. However, you need an element to attatch them to. So maybe you have...

    Code:
    <p id="p1">...stuff...</p>
    <p id="p2">...more stuff...</p>
    You could use javascript to apply one of the page-break rules to say the paragraph with id="p2". There are a few different rules for a page break. page-break-before, page-break-after, and page-break-inside. I tend to use page-break-after.

    To attatch it in javascript try something like...
    Code:
    <script type="text/javascript" language="javascript">
    <!--
    function breakin(){ //shameless 80's dance movie reference
    if(document.getElementById){ //check for DOM.  might need to make it more complicated for *****y browsers
    
       p=document.getElementById("p1");
    
    //The CSS rule is page-break-after, but in Javascript you have to reference it as pageBreakAfter
       p.style.pageBreakAfter="always"; 
    
    }
    }
    window.onload=breakin; //attatch it after the page loads.
    //-->
    Hope that helps!

    Cheers,
    Simon
    Last edited by sidvorak; 06-04-2004 at 06:50 PM.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,968
    Thanks
    0
    Thanked 236 Times in 233 Posts
    You can even do it without javascript.

    <style type="text/css">
    .newPage {page-break-before:always;}
    </style>
    ...
    <h2>Header 1</h2>
    <p>
    This is Paragraph 1 in Header 1.
    </p>
    <p>
    This is Paragraph 2 in Header 1.
    </p>

    <h2 class="newPage">Header 2</h2>
    <p>
    This is Paragraph 1 in Header 2.
    </p>
    <p>
    This is Paragraph 2 in Header 2.
    </p>

    Or if you want all h2 tags to be in new pages, you can simplify the CSS like this:
    <style type="text/css">
    .h2 {page-break-before:always;}
    </style>

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <style>
    .pagebreak{page-break-before:always;}
    </style>
    <br class="pagebreak">

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,968
    Thanks
    0
    Thanked 236 Times in 233 Posts
    What's the difference of that with my suggestion except of course of the tag used?

  • #7
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    there is no exception at all, I just forgot to write that I thought a break tag would be more appropriate to use doing a break.

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,968
    Thanks
    0
    Thanked 236 Times in 233 Posts
    IMO, you usually want a page break in between topics (usually preceded by <hn> tags) or paragraphs (<p>) or divisions (<div>) but probably rarely in newlines (<br>).


  •  

    Posting Permissions

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