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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    884
    Thanks
    53
    Thanked 6 Times in 5 Posts

    replacing a name with variable contents

    I'm sorry this is simple, but:

    Code:
    function printInput(f){
      var ifr = window.frames['printFrame'];
      var v1=readCookie('lesson');
      if (v1==1){v2='WORKNOTEPAD1'}
      if (v1==2){v2='WORKNOTEPAD2'}
      if (ifr){
         ifr.document.getElementById('content').innerHTML=f.v2.value.replace(/\n/g,'<br />');
         ifr.focus();
         ifr.print();
      }
      else { something }
    }
    In this line:

    Code:
    ifr.document.getElementById('content').innerHTML=f.v2.value.replace(/\n/g,'<br />');
    How can I make it stop erroring and act as if the "v2" were, say, "WORKNOTEPAD2"?

    If the line contains the string it all works fine.

    It must be so simple...
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,643 Times in 4,605 Posts
    Code:
    ifr.document.getElementById('content').innerHTML=f[v2].value.replace(/\n/g,'<br />');
    You can refer to form elements dynamically by treating them as part of the <form> collection. Which in JS or course means using [ ].
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,643 Times in 4,605 Posts
    You could actually do this much simpler:
    Code:
    function printInput(f){
      var ifr = window.frames['printFrame'];
      var v1=readCookie('lesson');
      if (ifr){
         ifr.document.getElementById('content').innerHTML
              = f["WORKNOTEPAD"+v1].value.replace(/\n/g,'<br />');
         ifr.focus();
         ifr.print();
      }
      else { something }
    }
    That is, if you are 100% sure v1 will contain either 1 or 2.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    884
    Thanks
    53
    Thanked 6 Times in 5 Posts
    Something's still amiss...

    It works when I don't use the variable and write:

    Code:
    ifr.document.getElementById('content').innerHTML=f.WORKNOTEPAD2.value.replace(/\n/g,'<br />');
    but it errors if I use:

    Code:
    ifr.document.getElementById('content').innerHTML=f[v2].value.replace(/\n/g,'<br />');
    If I alert(f[v2]); I get undefined.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,643 Times in 4,605 Posts
    ??? f *is* a reference to the <form>, yes?

    Well, you could try the older form:
    Code:
    ifr.document.getElementById('content').innerHTML
      =  f.elements[v2].value.replace(/\n/g,'<br />');
    Humor me and put
    Code:
    alert(v2);
    just BEFORE that line. Ensure that v2 is what you expect it to be.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,643 Times in 4,605 Posts
    For that matter, you can also do
    Code:
        alert( f.innerHTML );
    to be sure f is what you think it is.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    tpeck (11-12-2010)

  • #7
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    884
    Thanks
    53
    Thanked 6 Times in 5 Posts
    I like the simplified version but it still errors.

    And yes, v1 will always have a value read from the cookie.

    I'm mystified.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #8
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    884
    Thanks
    53
    Thanked 6 Times in 5 Posts
    I'm ashamed to tell you the reason for the error:

    Here's what finally works:

    Code:
    function printInput(f){
      var ifr = window.frames['printFrame'];
      var v1=readCookie('lesson');
      if (v1==1){v2='WRKNOTEPAD1'}
      if (v1==27){v2='WRKNOTEPAD2'}
      if (v1==38){v2='WRKNOTEPAD3'}
      if (v1==66){v2='WRKNOTEPAD4'}
      if (v1==97){v2='WRKNOTEPAD5'}
      if (ifr){ //print the content of the invisible iframe
         ifr.document.getElementById('content').innerHTML = f[v2].value.replace(/\n/g,'<br />');
         ifr.focus();
         ifr.print();
      }
      else {
    I had written WORKNOTEPAD instead of WRKNOTEPAD (hides face ).

    But I couldn't have done it without your help. And I don't think I can write it any simpler given that v1 is not consistent.

    Maybe I should put "var" before the v2 variable? But it still works without.

    Actually this whole print notepad routine still doesn't work in Chrome or Safari.

    Given that it works in IE AND FF, does anyone know if there is some obvious browser reason why the printout would fail in the other two browsers?

    Here is my cutdown demo:

    http://aapress.com.au/ieltsonline/02...ESSON1try.html

    I've shown it to quite a few people so I might have to settle for a warning sign (not Chrome or Mac compatible), but that's awful.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Did you try alerting the value of f? It looks like this.form on input type="image" is not valid. Since there's a js error, the form gets submitted so you don't see the js error in the console. Try changing it to

    Code:
    <input type="image" style="border: none;" src="../../img/printnotes.gif" onclick="printInput(document.FORMNOTES);return false;" />
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    And I would change the printInput function to:

    Code:
    function printInput(f){
      var v1=readCookie('lesson');
      if (v1){
         var nums = {'1':1, '27':2, '38':3, '66':4, '97':5};
         var field = f['WRKNOTEPAD' + nums[v1]];
         if (field){
              var ifr = window.frames['printFrame'];
              ifr.document.getElementById('content').innerHTML = field.value.replace(/\n/g,'<br />');
              ifr.focus();
              ifr.print();
         }
      }
      else{
          //...
      }
    }
    Last edited by glenngv; 11-12-2010 at 07:43 AM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #11
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    884
    Thanks
    53
    Thanked 6 Times in 5 Posts
    I've change "this.form" to "document.FORMNOTES" and added the alert to the demo.

    Sorry to report Chrome and Safari still refresh the page instead of doing what FF does and open a window to print from. (IE uses the iframe.)
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #12
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Change iframe window reference from:

    Code:
    var ifr = window.frames['printFrame'];
    to:

    Code:
    var ifr = document.getElementById('printFrame').contentWindow;
    Your code doesn't work in FF as opposed to what you said. My suggestion should work in IE, FF and Chrome.


    And also, try my suggestion in my previous post to improve the printInput function and make it work with dynamic textarea names. Just change the iframe window reference accordingly.
    Last edited by glenngv; 11-12-2010 at 08:42 AM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • Users who have thanked glenngv for this post:

    tpeck (11-12-2010)

  • #13
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    884
    Thanks
    53
    Thanked 6 Times in 5 Posts
    I can't believe after all this time it works in Chrome and Safari - and in all browsers including FF it now goes straight to the print dialog box - as it does in IE. I can't check that it actually prints because my printer is dead, but I will tomorrow.

    In FF v3.6.12, without your last suggestion, FF was opening a window and printing from there. I assumed that was expected given the "else" option. The error console was not showing an error either, so I don't know what was going on.

    Thank you so much!

    I read somewhere that there is an issue with FF etc, not printing out all the contents of the textarea if all the contents cannot be displayed, but I have a potential solution to that - and now the thing actually works I will test for that display issue tomorrow. If it does turn out to be a problem, hopefully, I will be able to post the entire solution to cross-browser printing out of a textarea soon.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #14
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    884
    Thanks
    53
    Thanked 6 Times in 5 Posts
    Not sure if this is important, but on my machine - even with glenngv's corrections - the routine will error in Chrome if the iframe is not situated within the form. In FF, if the iframe is outside the form, it brings up the window to print from.

    In IE, the iframe can be inside or outside the form and it always brings up the print dialog with no window needing to be created.

    And thanks again - that neater solution to the printInput function works like a charm!
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #15
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    I searched this forum and found out that the print textarea script you are using came from the suggestion I made more than 6 years ago from the below thread

    http://www.codingforums.com/showpost...86&postcount=7

    That explains the feeling I had that your code looks familiar the moment I saw it
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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