TPAC: Autosuggest focus() throws a JS error
authorDan Scott <dscott@laurentian.ca>
Mon, 30 Jul 2012 15:30:50 +0000 (11:30 -0400)
committerDan Scott <dscott@laurentian.ca>
Fri, 17 Aug 2012 18:15:13 +0000 (14:15 -0400)
On page load, the Dojo autosuggest code tries to set focus to the
element with the ID "search_box" - which is fine, except when the page
does not have said ID. Therefore, test for the existence of the ID
before setting focus to it.

Signed-off-by: Dan Scott <dscott@laurentian.ca>
Signed-off-by: Art Rhyno <art632000@yahoo.ca>

Open-ILS/src/templates/opac/parts/js.tt2

index e4aabb3..b4a775d 100644 (file)
 
     /* Set focus, and place the cursor at the end of the input string */
     dojo.addOnLoad(function() {
-        dijit.byId('search_box').focus();
+        /* Don't error out if the object doesn't exist, like on advanced search pages */
+        if (dojo.byId('search_box')) {
+            dijit.byId('search_box').focus();
 
-        var sb_value = dijit.byId('search_box').value;
-        /* Dojo won't trigger a change if the value doesn't change */
-        if (sb_value) {
-            dijit.byId('search_box').setValue(sb_value + ' ');
-            dijit.byId('search_box').setValue(sb_value);
+            var sb_value = dijit.byId('search_box').value;
+            /* Dojo won't trigger a change if the value doesn't change */
+            if (sb_value) {
+                dijit.byId('search_box').setValue(sb_value + ' ');
+                dijit.byId('search_box').setValue(sb_value);
+            }
         }
     });
 </script>