Question

I have events that I'm e.preventDefualt() within the event handler. But for the couple of seconds before the page loads that javascript file and it executes, the user has the ability to fire that event with default. Whats the best way to prevent this? Should I go old school and prevent it with an onload="return false" attribute, this way it can't submit? Should I use a button instead of a submit event button?

The gist

  • page loads
  • user submits before js loads
  • form isn't prevented default
  • unintended consequences

How do I fix this?

Was it helpful?

Solution

Hide the form by (inline) CSS (display: none) and show it with JavaScript (document.getElementById(...).style.display="block"). Place the JavaScript in the same file as your event handlers.

That way the form will be hidden until the Javascript has been loaded. But actually, you should not rely on Javascript to prevent 'unintended consequences'. Users can always disable Javascript.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top