diff --git a/src/main/resources/org/richfaces/renderkit/html/scripts/focus.js b/src/main/resources/org/richfaces/renderkit/html/scripts/focus.js index 4cce423..4b94291 100644 --- a/src/main/resources/org/richfaces/renderkit/html/scripts/focus.js +++ b/src/main/resources/org/richfaces/renderkit/html/scripts/focus.js @@ -1,40 +1,50 @@ if (!window.Richfaces) window.Richfaces = {}; -Richfaces.FocusManager = (function(){ +Richfaces.FocusManager = (function() { - var m_focus; - var m_priority = 999999; - var eventAttached = false; + var m_focus; + var m_priority = 999999; + var m_domLoaded = false; - var attachEvent = function() { - if(!eventAttached) { - document.observe('dom:loaded',function(){ - var element = $(m_focus); - element.focus(); - element.select(element); - Richfaces.FocusManager.clearFocus(); - }); - } - eventAttached = true; - }; + var focus = function() { + if (m_focus == null) { + return; + } + try { + var element = $(m_focus); + if (element != null) { + element.focus(); + element.select(element); + } + } finally { + Richfaces.FocusManager.clearFocus(); + } + }; - return { - getFocus : function() { - return m_focus; - }, - setFocus : function(id,priority) { - if(priority == null) { - priority = 99999; - } - if(m_focus == null || priority < m_priority) { - m_focus = id; - m_priority = priority == null ? 0 : priority; - attachEvent(); - } - }, - clearFocus : function() { - m_focus = null; - m_priority = 999999; - } - }; - })(); + document.observe('dom:loaded', function() { + focus(); + m_domLoaded = true; + }); + + return { + getFocus : function() { + return m_focus; + }, + setFocus : function(id, priority) { + if (priority == null) { + priority = 99999; + } + if (m_focus == null || priority < m_priority) { + m_focus = id; + m_priority = priority == null ? 0 : priority; + if (m_domLoaded) { + focus(); + } + } + }, + clearFocus : function() { + m_focus = null; + m_priority = 999999; + } + }; +})();