Friday, August 12, 2011

DOM Button Elements

While working on one of my projects I ran into a problem using the javascript:history.go(-1) script in an onclick event handler. After clicking the button my page wouldn't change.

After spending a while trying to figure why Chrome was the only browser to do this, my friend Jeff and I discovered the cause of our problem was the <button> tag. After further investigation we found that <button> by default has its type set to "submit" which causes postback behavior.
In order to work around this you have to specify the type as you would in an form input field: type="button".

We noticed in Chrome at least, that the button didn't cause any postback behavior when it wasn't wrapped in form tags.