We've all seen those edit pages in Salesforce when you access the page and a date or date/time field calendar widget immediately pops up. Sometimes that's okay but sometimes we would like to prevent that from happening because we would prefer the focus to be on some other element of the page. As a Visualforce developer you can accomplish this with a very simple snippet of JavaScript.
If you ever looked at the source code of an edit page in Salesforce you would find that there is an onload event listener within the body tag of the page. This tells the page to execute some JavaScript after the page is loaded by the browser. That executed JavaScript will eventually call a function named setFocusOnLoad(), which is a bit of code that tells the browser to focus on a particular element within the page. Typically the focus is given to the first input element on the page. If the first input element of the page is a date or date/time field then the calendar widget will actually be rendered. The calendar will actually render even if the date or date/time field is not first in the page as long as any input elements prior to it are picklists or inputs that can't actually have a cursor placed within them.
Nonetheless, the need may arise when you prefer that the focus is removed from an input element. The easiest way to accomplish this is to create a JavaScript function with the same name as the setFocusOnLoad function but have no logic within it. See below:
Applied to a Visualforce page you can now see that the focus is removed from the date field when the page is loaded.
Author
Greg Hacic
I've been working with Salesforce since 2003. Over the years, I've held various roles for a diverse array of salesforce.com customers, created a Salesforce specific ISV, and built numerous applications for the AppExchange. All of these experiences have allowed me to learn quite a bit about building on the platform.