Hidden Layer Print Problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bonnett

    Hidden Layer Print Problems

    I have a piece of hidden text that the user can show/hide by clicking
    on a link, the problem I have is that I would like the text to appear
    when the page is printed.
    I've used the @media print in my CSS, and this shows the text when the
    page is printed without clicking on the link. However, if you show then
    hide the text (by clicking the link twice) the text does not appear in
    the print preview. It's as if the javascript is overriding the @media
    rule. Is there any other way to achieve what I am trying to do, or is
    it going to be impossible to display the hidden text when printed.

    I've prepared a test page here:


    I've have tested this on Firefox 1.0 and IE6, and any solution will
    only have to work on these browsers as the page is for a company
    intranet.

    Bonnett

  • Ivo

    #2
    Re: Hidden Layer Print Problems

    "Bonnett" wrote[color=blue]
    > I have a piece of hidden text that the user can show/hide by clicking
    > on a link, the problem I have is that I would like the text to appear
    > when the page is printed.
    > I've used the @media print in my CSS, and this shows the text when the
    > page is printed without clicking on the link. However, if you show then
    > hide the text (by clicking the link twice) the text does not appear in
    > the print preview. It's as if the javascript is overriding the @media
    > rule. Is there any other way to achieve what I am trying to do, or is
    > it going to be impossible to display the hidden text when printed.
    >
    > I've prepared a test page here:
    > http://www.pete-b.co.uk/printProblem.htm
    >
    > I've have tested this on Firefox 1.0 and IE6, and any solution will
    > only have to work on these browsers as the page is for a company
    > intranet.[/color]

    The initial hide is done with a class, then the javascript modifies the
    style.display directly. Of course that overrides the properties set by the
    class: it is more specific and will win the CSS cascade.
    If you have the javascript toggle a className (by the way, elements can have
    more than one classes, eparate them with commas) instead of the style
    itself, then all should be well.

    This function should give some clues:

    function toggleclass(el, name){
    if(el.className ===''||el.class Name.indexOf(na me)<0) {
    el.className+=' '+name;
    } else {
    el.className=el .className.repl ace(name,'');
    }
    }
    --
    Ivo


    Comment

    • Michael Winter

      #3
      Re: Hidden Layer Print Problems

      On Thu, 25 Nov 2004 12:40:59 +0100, Ivo <no@thank.you > wrote:

      [snip]
      [color=blue]
      > (by the way, elements can have more than one classes, eparate them with
      > commas)[/color]

      You mean separate them with *spaces*. Your code does that, but I thought I
      should make sure there's no confusion.

      [snip]

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • Ivo

        #4
        Re: Hidden Layer Print Problems

        "Michael Winter" wrote[color=blue]
        > On Thu, 25 Nov 2004 12:40:59 +0100, Ivo <no@thank.you > wrote:
        >[color=green]
        > > (by the way, elements can have more than one classes, separate them
        > > with commas)[/color]
        >
        > You mean separate them with *spaces*. Your code does that, but I thought I
        > should make sure there's no confusion.
        >[/color]
        Darn. Typing too quickly again...
        Thanks for pointing that out,
        Ivo


        Comment

        • Bonnett

          #5
          Re: Hidden Layer Print Problems

          Cheers, that was just what I was after, I've adapted your function to:

          function showHide(id){
          element = document.getEle mentById("descr iption"+id);
          element.classNa me = (element.classN ame == "show") ? "dontShow" :
          "show";
          }

          with the CSS as:
          ..dontShow { display:none}
          ..show {display: inline }
          @media print {
          ..dontShow { display:inline}
          }

          and the HTML to:
          <a href="#" onclick="showHi de(0); return false"
          onkeypress="sho wHide(0); return false">Show Hidden Text</a>
          <span class="dontShow " id="description 0">
          This is some hidden text
          </span>
          (I've used a unique number as the page will have numerous hidden parts)

          Comment

          • Ivo

            #6
            Re: Hidden Layer Print Problems

            "Bonnett" wrote[color=blue]
            > Cheers, that was just what I was after, I've adapted your function to:
            >
            > function showHide(id){
            > element = document.getEle mentById("descr iption"+id);
            > element.classNa me = (element.classN ame == "show") ?
            > "dontShow" : "show";
            > }[/color]

            Excellent. Only one more thing to make it perfect: "element" is now made a
            global variable, a useless waste of memory. If you precede it in the first
            statement with the keyword "var" it will be a local variable, existing only
            in the the scope of the function. This will not affect what happens on
            screen or on paper or anywhere else, but on a large page with many elements
            with many id's and many variables, you may notice a difference in
            performance. Like so:

            var element = document.getEle mentById("descr iption"+id);

            --
            Ivo


            Comment

            • Bonnett

              #7
              Re: Hidden Layer Print Problems

              Ta very much, I've always wondered about the difference between whether
              the "var" is specified or not.

              --
              Bonnett

              Comment

              • Daniel Kirsch

                #8
                Re: Hidden Layer Print Problems

                Bonnett wrote:[color=blue]
                > I've used the @media print in my CSS, and this shows the text when the
                > page is printed without clicking on the link. However, if you show then
                > hide the text (by clicking the link twice) the text does not appear in
                > the print preview. It's as if the javascript is overriding the @media
                > rule. Is there any other way to achieve what I am trying to do, or is
                > it going to be impossible to display the hidden text when printed.[/color]

                You may try:

                @media print {
                .described { display:inline !important }
                }

                Daniel

                Comment

                Working...