Tuesday, April 5, 2011

onsubmit and Chrome

I happened to take a peek at my javascript oracle from my netbook -- on which I use Google Chrome -- and I was shocked to find out that the application wasn't working. Or rather, it was working, but the page was reloading for no reason. I was pretty confused by it, as the application in question works just fine from Firefox, Internet Explorer, and Opera. So, I did some investigating, and found out the root of the issue:

Chrome seems to reload (or perhaps restore?) the page to its original state after a function is called with onsubmit from a form. If you're whole plan is based on the altered page staying up for the user to read... that's probably not what you intend. Here's what I did:

var chrome=navigator.userAgent.search("Chrome")
if (chrome != -1)
{ event.preventDefault(); }

Of course, event.preventDefault(); is the workhorse there. However, Firefox started messing up on me with that in the code, so I put it one of those nifty if statements.

Addendum (several days later): I was fiddling with that same oracle earlier, and I realized that this post is probably wrong. It seems far more likely that Chrome is reacting to the document.write(); I'm calling on and not onsubmit. At any rate, it worked like a charm. :)

No comments:

Post a Comment