Hacker News new | ask | show | jobs
by leepowers 4906 days ago
An important caveat to the "load scripts at the end of the document technique": The visual elements of a page will render before the functional components (i.e., JavaScript) are available.

Imagine a site the sets a custom `submit` handler for a `<form>` tag, possibly for client-side form validation, an AJAX file uploader, date selection, etc. A user submits the form before JavaScript has loaded completely and doesn't get the benefit of the JavaScript-enhanced experience. In local development or on a high-speed connection this will probably never happen. But a user accessing the site with their phone over the slow U.S. cell data network has a much higher chance of hitting upon this undesired behavior.

1 comments

Would it be acceptable to set the form's submit button to disabled in the HTML, attaching the custom event handler and enabling the submit button in a document-final script? Seems like you still get reasonable behavior in the low-bandwidth environment -- maybe even better, since the user can now access the other elements of the form while the script is being loaded and run.
That's an accessibility issue for users with JavaScript disabled (or where JavaScript fails to load).

On the other hand, it's also the recommended method to protect your users from a click-jacking attack across all browsers (read those that don't support CORS).