Stec Records Forum - Discussions about software and hardware used to make music.
gfxgfx
 
Welcome, Guest.

Login with username, password and session length

December 18, 2017, 10:22:46 AM
logo
gfxgfx
gfxgfx
 
If you registered as a user but have not been approved, your email address or IP address may be listed at http://www.stopforumspam.com and was rejected.  Contact us (http://www.stecrecords.com/contact.php) if you believe this was in error.
gfxgfx

gfx* Home Help Search Calendar Login Registergfx
The Stec Records Forum  |  Software  |  Development  |  Javascript  |  Dynamically created Javascript form triggers a page reload « previous next »
Pages: [1] Go Down Print
Author Topic: Dynamically created Javascript form triggers a page reload  (Read 1080 times)
bsellon
Chief Administrator
Administrator
Hero Member
*****
Offline Offline

Posts: 770


Time reveals truth.


WWW Email
« Reply #1 on: October 01, 2014, 12:08:08 PM »

Problem:
I dynamically created a search form using Javascript  with an onclick handler for a "Begin Search" button assigned to a function that performed the search.  I'd used  a div for the form in the past but if you use an actual form entity, the browser will provide the user with previous values. This is really helpful on log-in forms.  I'm trying to eliminate or minimize page reloads so the search happens in the background. The search actually uses the same query mechanism used to load the default page content but was crashing the page (or so I thought) when run from my search form; it got part way through the http request when the page would suddenly reload.

Solution:
It seems so obvious now but it took me an embarrassing amount of time to figure out, which is why I'm posting this to the forum (for others and my future-self).

It turns out that the page was not, in fact, crashing but instead performing the standard load all forms do if you click a button in them.  I thought that I was trapping the onclick event with my function but forgot that the event gets forwarded along to the browser triggering the page reload.  I was so sure it was crashing that I was ignoring expected behavior.

The fix was easy (of course) though not obvious if you're at all new to Javascript events: add "return false" to the end of the onclick function. This tells the browser to discard the event instead of processing it (i.e. using it to Submit the form). 
Logged
The Stec Records Forum  |  Software  |  Development  |  Javascript  |  Dynamically created Javascript form triggers a page reload « previous next »
Pages: [1] Go Up Print 
Jump to:  
Powered by MySQL Powered by PHP SMF 2.0.2 | SMF © 2011, Simple Machines
Apollo design by Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.14 seconds with 21 queries.