Pat Gunn (dachte) wrote,
Pat Gunn

JS Ugliness

I'm weirded out that standards for Javascript programming are so low. Having made a more-or-less a successful first project, where I really rushed to get it done, cut corners, and just did the first thing that came to mind (that worked) to solve problems, I'm going back and refactoring it now, in some cases trying to learn best practice with some good documentation.

I'm sad that it's not actually possible to make things clean, and one of the kludges I am least proud of seems to be standard practice.

Ordinarily with a form in a page, you have a bunch of form elements, and you have some way to submit the form, which causes your browser to scoop up the state of all those form elements, bring you to a few webpage, and give the webpage the state of that form in some fashion (I could explain, but let's gloss over those details).

One of the points in my demo is that I want to be able to do forms that talk to javascript that manipulate the currently loaded page and then reset themselves, rather than loading an entirely new page. This is fun because it doesn't take anything on the webserver end, and in fact doesn't even require a webserver; I'm doing development with a flat HTML file with all the javascript packed into it, using a file:/// url.

Still, forms are useful; the widgets they provide are really useful. So, how do I use those widgets but prevent the form from ever doing a submit?

My hackish solution was to cheat; if you view the source of my demo, you'll find that I hook "onSubmit" events, use it as the main entry point into my code, and then return false, using a mechanism normally used by form validation to back out of the submission. I also have code to clean up the entry box. Ugly, but ok for a proof of concept. Or so I thought. I was shocked to find that my ugly hack seems to be standard practice. WTF. It's not even that robust; if your javascript doesn't pass muster with the parser, a submission will happen anyhow, when it probably should never happen. Highly uncool. I'm looking for better solutions, but really bothered that the status quo sucks so much.

Tags: tech

  • Still alive

    Been feeling a bit nostalgic. Not about to return to LiveJournal - their new ownership is unfortunate, but I wanted to briefly note what's been up…

  • Unplugging LJ

    It's about time I pulled the plug on the LJ version of my blog: 1) I'm much more active on G+ than I am with general blogging. I post many times a…

  • Mutual Trust

    I don't know which should be considered more remarkable: That a cat should trust a member of a far larger and stronger species that it can't…

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded