Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-14-2010, 03:57 AM   PM User | #1
tpeck
Regular Coder

 
tpeck's Avatar
 
Join Date: Oct 2002
Location: Sydney, Australia
Posts: 771
Thanks: 40
Thanked 5 Times in 4 Posts
tpeck is on a distinguished road
printing text

I have a routine that prints a textarea:

Code:
function printInput(f){
  var ifr = window.frames['printFrame'];
  if (ifr){ //print the content of the invisible iframe
     ifr.document.getElementById('content').innerHTML=f.WRKNOTEPAD.value.replace(/\n/g,'<br />');
     ifr.focus();
     ifr.print();
  }
  else { //print by opening a new window and then closing it
     var html='<html><head><style type="text/css">div{font:normal 14px Verdana}</style></head><body onload="window.print();window.close()"><div>'+f.WRKNOTEPAD.value.replace(/\n/g,'<br />');+'</div></body></html>'
     var win = window.open('','_blank','menubar,scrollbars,resizable');
     win.document.open();
     win.document.write(html);
     win.document.close();
  }
}

function writeContent(objIframe){
  var html='<html><head><style type="text/css">div{font:normal 14px Verdana}</style></head><body><div id="content"></div></body></html>'
  objIframe.document.write(html);
  objIframe.document.close();
}
This works nicely when the button is of input type:

Code:
<form name="FORMNOTES"><textarea name=WRKNOTEPAD cols=140 rows=20 wrap=yes style=\"font-family: Arial; font-size: 10pt\"></textarea><div>
<br>
<input type="button" value="Print notepad" onClick="printInput(this.form)">
</div>
<iframe name="I1" id="printFrame" src="javascript:parent.writeContent(this)" width="1" height="1"></iframe></form>
But when I try to make the input button an img button it errors:

Code:
<img border="0" src="/images/printnotes.gif" onClick="printInput(this.form)">
The error is "WRKNOTEPAD is null or not an object".

Is there any way to overcome this and use an image to achieve the same thing? I'm thinking that the image is not an element(?) - is that the reason?

Thanks for any light on this.
__________________
The difference between genius and stupidity is that genius has its limits. (Albert Einstein)
tpeck is offline   Reply With Quote
Old 07-14-2010, 06:24 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Ummm...that is *NOT* how you specify an image button!

That is simply an ordinary <img> object and it has NO CONNECTION AT ALL to the <form>!!! That is "this.form" is null for it!

Try this:
Code:
<img border="0" src="/images/printnotes.gif" onClick="printInput(document.FORMNOTES)" />
Or you really *can* use an image button thus:
Code:
<input type="image" style="border: none;" src="/images/printnotes.giv"
       onclick="printInput(this.form);return false;" />
Don't forget the return false, or the image button will automatically submit the <form>.
__________________
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.
Old Pedant is offline   Reply With Quote
Old 07-14-2010, 08:56 AM   PM User | #3
tpeck
Regular Coder

 
tpeck's Avatar
 
Join Date: Oct 2002
Location: Sydney, Australia
Posts: 771
Thanks: 40
Thanked 5 Times in 4 Posts
tpeck is on a distinguished road
I see now. Thanks. I wish I could have worked it out myself. Now I have to commit the logic to memory!
__________________
The difference between genius and stupidity is that genius has its limits. (Albert Einstein)
tpeck is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:12 PM.


Advertisement
Log in to turn off these ads.