0

I have made an assessment portal using visualforce pages and sites. It is something that is a mock DEV401 exam. But I am facing a problem. At the end of the test, when user submits his test, a confirm box is rendered using javascript 'onClick'. Most of the times, it accepts the user response perfectly, but sometimes, even if the user clicks 'OK', the page does not navigate to the next page that shows the result, but rather refreshes the current page where the timer resets to 90 minutes again. Can anyone please tell me what must be going wrong in it?

Thanks in advance!

1
  • 1
    You should try to identify a reproducible test case for the failing before any further investigation could be done. For instance, it happens only on IE and Firefox, or it happens only if some question is left unanswered, etc etc. Problem could be in your logic as well could be some browser pitfall. Pasting of the interested part of your code would help us help you. – MLucci Nov 12 '14 at 9:11
1

Ensure that the onClick event is prevented from bubbling up - that would submit your form and cause the page refresh.

If you are using a "simple" onclick (inline HTML attribute or jsElement.onclick = function (){}) then is enough to ensure the function is always returning a "false" value. (See https://stackoverflow.com/questions/128923/whats-the-effect-of-adding-return-false-to-an-onclick-event )

If you are using jQuery - $(el).click(function (e){}) - then you can add in your event block code e.preventDefault() and e.stopPropagation() to prevent event to bubble up to parent elements, or cause other effects than the ones you want in your logic. (See http://api.jquery.com/event.preventdefault/ and http://api.jquery.com/event.stoppropagation/). But even in jQuery case, returning false should be enough maybe.

3
  • I'm using onclick on the command button. This refers to the javascript function which pops the prompt box. An 'OK' on this prompt box takes the user to the next page. For some users that is happening correctly. But for others, it just refreshes the page and the timer resets without going to the next page. When I removed the javascript, it worked fine because then it was just visualforce navigation from one page to another. But with javascript prompt box, it shows a problem. – Sam Nov 13 '14 at 11:03
  • @Sam I would be available to help you if you can send over the faulty and working code - are you able to put up a GIST for us? (If you still have the issue of course.) – MLucci Nov 13 '14 at 17:01
  • JavaScript onclick event not behave properly with commandButton. I can guess u have defined action there as well.. this may be the culprit. Just try to remove commandButton and use html button. Surely you would get rid of this problem – Ankuli Apr 26 '15 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.