Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Submitting form after n seconds using JavaScript  RSS feed

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys -

I've come up with a way that I can do form submission after a specified number of seconds that is reliable enough for our purposes in Firefox. Unfortunately, the technique fails in all other browsers that I've tested and I can't seem to figure out why.

Refresh Code (the timer checks/values are all set by JSP - checking the page source before the form submission shows the same code in all browsers, so I think I can rule out my JSP as the culprit here):


JavaScript:


HTML (if timing is needed):


HTML form definition:


Basically, I set a timer variable that determines whether a meta refresh tag should be placed, and what its value should be. This is important because I need the timer to use externally specified times. Since I know that the page is going to refresh (and hence, trigger the onunload event), I warn users not to use back/forward/refresh commands, and have the onunload event trigger a form submission. For some reason, in Safari, Chrome, and IE, the code on the submitted form's target page never executes, even though the alert in the submitForm() function is triggered.

Does anyone have any idea what might be going on here? My method seems pretty simple, and I'm having a hard time seeing what the issue might be.

Thanks for any help.
 
Author and ninkuma
Marshal
Posts: 66729
167
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not just use window.setTimeout()?
 
Sam Gardner
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Why not just use window.setTimeout()?



Well, I can't figure out how to dynamically set the number of miliseconds while doing that.

Right now, the refresh rate is set via user editable config files (this project is designed so that parts can be added without someone having to modify the application itself). As far as I know, I can't easily get access to those values (currently stored in session variables) from JavaScript, whereas its very easy to access them from the JSP code (and hence, easy to dynamically emit a meta tag with the correct parameters).
 
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is it any different setting it in the meta refresh as it is setting the JavaScript in the same page?

Eric
 
Sam Gardner
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Eric Pascarello wrote:How is it any different setting it in the meta refresh as it is setting the JavaScript in the same page?

Eric



Well, this is basically the JSP code I have now:



If you can tell me how to access that session variable, only add a timeout if it isn't null (or an invalid value that I can set or something like that), and add the timeout for the number stored in the session variable, then I can add the window timeout. Since JavaScript is client-side I couldn't see an easy way to access the server-side session variables.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66729
167
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric's point is if you can use the scriptlet expression in one place in the JSP, why can't you use it elsewhere?

(Hint: the answer is there's no reason).
 
Sam Gardner
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Eric's point is if you can use the scriptlet expression in one place in the JSP, why can't you use it elsewhere?

(Hint: the answer is there's no reason).



Oh, heh.

I didn't even think of emitting JavaScript with the JSP expression - too meta for me I guess. I'll give it a shot - thanks for the help.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66729
167
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To a JSP, anything that's not a JSP element is just template text. It could care less...
 
Sam Gardner
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:To a JSP, anything that's not a JSP element is just template text. It could care less...



Ah, well, now that I think about it it makes sense.

Getting the formatting right on the scriptlet is proving to be a royal PITA though.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66729
167
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see why... but in any case, scriptlets are a pain in the rear and shouldn't be used in new code. You'd find modern JSP with the EL much easier to deal with.
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!