2

Possible Duplicate:
When should I use Inline vs. External Javascript?

Often Javascript needs to be run as soon as possible. For example, suppose I have some radio buttons in a form and when the form fails to submit, the Javascript selects the last button that I selected. If one button is selected by default and the user sees this and then the Javascript changes buttons, it will look weird. Therefore, the script should run as soon as possible and it seems that inlining Javascript might help with this. Is this likely to make a significant difference in terms of reducing how often users see this kind of weird behaviour?

Community
  • 1
  • 1
Casebash
  • 114,675
  • 90
  • 247
  • 350
  • Another option is to generate the HTML dynamically, however this isn't always possible or worth the effort – Casebash Oct 05 '11 at 00:35
  • 1
    Inline script is available as soon as the element is, so yes, it will reduce delays and may be noticeably faster than dynamcially adding listeners, depending on how many need to be added and the logic involved. – RobG Oct 05 '11 at 00:41

1 Answers1

1

No, you can't guarantee that your Javascript code will run in time to prevent the user noticing the change, no matter where you put it.

The correct way to do this is to modify the HTML of the form sent from the server, to indicate the user's previous choice as the selected radio button.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 1
    Inline script is available as soon as the related element is, what other delay is there? – RobG Oct 05 '11 at 00:42
  • I know that there is no guarantee, but I am curious as to whether inlining will make a significant difference – Casebash Oct 05 '11 at 00:51