I noticed a form on one of my web apps was submitting instead of calling a Javascript function, but only on any iOS browser (WebKit). There was no submit button on the form, only an anchor with a onclick="submit()"
attribute. It appears that in WebKit, the form's submit()
method is being called instead of my own.
Here is a sample code that reproduces the problem:
<html>
<body>
<form id="my-form">
<a onclick="submit()" href="#">Test</a>
</form>
<script>
function submit() {
console.log("Hello!");
}
</script>
</body>
</html>
Running this on a non-WebKit browser will just output "Hello!" on the console. Running it on a WebKit browser will do a refresh of the page.
I created a more comprehensive example on JS Bin. In that example I added an event listener for the submit event of the form and a hidden input field. I did not include the submit()
function. As you will see, when clicking on the link on a WebKit browser the page will refresh and you'll notice the URL change. But on other browser it won't and will output the error of the missing submit()
method on the console. What's is very odd is that, in WebKit, the page will refresh as if the form was submitted but the event listener will not trigger.
Changing the function's name solved the issue for me. But I wonder why would it behave differently in WebKit. My best guess is that WebKit is calling the form's submit
method. But I do not know for certain.