javascript - Different behavior of the onclick attribute during form submission -


hello came across weird behavior onclick attribute regarding form submission.

page on server:

<!doctype html public "-//w3c//dtd xhtml 1.0 strict//en"         "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head lang="en-us" >     <meta http-equiv="content-type" content="text/html; charset=utf-8" />     <title>return form example</title>     <script type="text/javascript">         function dosomething(){             alert("hey");             return false;         }     </script> </head>  <body>     <form action="submit.php" method="post">         <input type="submit" onclick="return dosomething()" value="click me!">     </form> </body> </html> 

in example, running on server, when click submit button alert saying hey , stay on current page. however, tried set same example on jsfiddle , 404 error meaning form submitted. cannot figure out why occurs.

here jsfiddle trying replicate behavior on server.

jsfiddle: http://jsfiddle.net/46xsv/

you want check option "no wrap - in <head>" "do not wrap javascript code, place in section".

select "no wrap - in <head>" under "framework , extensions"

in page you'll find description of each of options around bottom: http://doc.jsfiddle.net/basic/introduction.html.

also practice include semicolon @ end of return statement, following:

<input type="submit" onclick="return dosomething();" value="click me!"> 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -