39

Well i have a asp.net page where i have a button that im using to execute a JavaScript... but after the JavaScript has run the page reloads(postback) how can i avoid that?

<button onclick="printElement('content');">Print</button>
4
  • i tried to return false from the printElement function and that had no effect..
    – Peter
    Nov 4, 2010 at 11:31
  • did you add a return in front of the call to printElement?
    – Paddy
    Nov 4, 2010 at 11:32
  • well placing the return false in the printElement function did not seem to change any thing but placing it like you 2 posted seemed to work thanks alot!
    – Peter
    Nov 4, 2010 at 11:37
  • If the form should be submitted if printElement returns true than simply return printElement('content'); Nov 4, 2010 at 11:37

5 Answers 5

67

you need to use return false

<button onclick="printElement('content');return false">Print</button>
4
  • 1
    +1 - this works just great. I just wanted to add that putting the "return false;" in your javascript function won't work, it has to appear after the method in the onclick declaration. I was hoping the opposite was true and it isn't :( Mar 6, 2013 at 22:27
  • 1
    after days of trying to get this to work, finally you serve it to us on a silver tray. Thanks
    – vbNewbie
    Jun 20, 2013 at 3:32
  • 1
    note if you use onclick="return printElement('content');" then you CAN use the return value, true or false, from the function. You just need to return something from the onclick event itself (which is itself wrapped up in a function before exection), or it defaults to true. Sep 19, 2013 at 2:22
  • OMG I spent 3 hours looking for "return false"!
    – wei
    Nov 17, 2018 at 4:10
51

Add type="button" to your <button> tag like so:

<button type="button" onclick="printElement('content');">Print</button>

This answer explains the behavior.

3
  • 2
    Unbelievable. I had the "type=button" part missing, and without it, "return false;" just had no effect. Thanks for the tip ! Jan 23, 2014 at 13:09
  • 2
    This is the correct answer. I don't understand why that other answer got so many upvotes.
    – Matthew
    Jun 18, 2015 at 16:11
  • 1
    Also, return false does nothing and is not needed. type=button is essential.
    – Roland
    Mar 14, 2017 at 10:35
7

Alter you click event as below:

<button onclick="printElement('content');return false;">Print</button>
4

if you don't need a post back you should use an HTML button.

in that case you don't need override the behavior to avoid the post back and it's laighter to load (it doesn't need to be worked by the server to render back the HTML)

2
  • HTML button can't raise a post back itself. Probabily you are using the asp.net version. <input type="button" value="something" onclick="alert('no postback')"> Nov 4, 2010 at 13:30
  • 1
    if it where an asp button i would write <asp:button not <button
    – Peter
    Nov 10, 2010 at 13:30
4

You should just use an input button instead:

<input type="button" onclick="printElement('content')" value="Print" />

This doesn't NEED an form (so you can use it without) and you don't have the problem with the postback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.