views:

151

answers:

4

I have a form that contains this single input field, which is nothing more than a button to Print the current web page:

<div align="center">
<input type="image" src="../Images/print.jpg" value="Print" onclick="printpage();" /></div>
</div>

After the printing, the page re-submits itself (to itself). Why does it do this and can I stop it?

If I just change the type to "input", this code does not re-submit itself after printing:

<div align="center">
<input type="input" src="../Images/print.jpg" value="Print" onclick="printpage();" /></div>
</div>

Unfortunately, our style conventions require me to use that button image rather than the standard input button.

+5  A: 

Change the onclick handler to onclick="printpage(); return false;" - that will prevent the button from doing anything besides running the JavaScript.

Aric TenEyck
Might be worth mentioning that the *reason* you need to do this is that an input element of type "image" is by default a *submit* button, and therefore submits the form when clicked.
Syntactic
+3  A: 

add 'return false;' to your onClick event:

onclick="printpage();return false;"
Ray
A: 

Have you tried adding a

return false;

at the end of your code therefor

<input type="input" src="../Images/print.jpg" value="Print" onclick="printpage();return false;" /></div>
Justin Gregoire
A: 

Thanks. 'return false;' did the trick

DreamU