Wednesday, September 3, 2008

SearchBoxEx, FireFox and the Enter Key

Here's a problem with SearchBoxEx and FireFox that's easy to reproduce.

1) Create an out-of-the-box Publishing Portal. No customizations required.

2) With FireFox 2 or 3, go to the home page of the new site.

3) In the search box, enter a search phrase and press the enter key.

4) The home page simply refreshes instead of going to the search results page.

With IE 6/7 and Safari 3, everything works as expected. That is, the search results page is displayed.

I've reproduced this bug with three flavors of MOSS: MOSS RTM, with MOSS SP1 installed and finally with MOSS Infrastructure Update installed.

Looking at the traffic in Fiddler, it appears that *two* requests are being generated by FireFox. One to the search results page and one to the home page (in that order).

So...what's the fix?

After using http://www.rotary.com/ as inspiration, we came up with the following JavaScript that replaces the default key press event handler provided by MOSS.

To use the script, you'll need to supply the correct name of the submit method and input control id that is dynamically generated by SharePoint. After you do that, wrap the code in a script tag and place it on the page.

function replaceOnKeyPress()
{
var theFunction = "SDXXXXXXX_Submit();";
var theInputID = "ctl00_PlaceHolderSearchArea_SearchBox_SDXXXXXXX_InputKeywords";

var txt = document.getElementById(theInputID);
var browser=navigator.appName;

if (txt && (browser == "Netscape"))
{
txt.onkeypress = function(e)
{
var key = String.fromCharCode(e.keyCode);

if (key == "\n" key == "\r")
{
eval(theFunction);
return false;
}
}
}
}

replaceOnKeyPress();

2 comments:

Anonymous said...

Thanks for your suggestion Mark.
I had the same problem, and I tried to use your code... and it doesn't work.
Then I tried with the code from http://www.rotary.org/en/Pages/ridefault.aspx and finally I got my search box working in firefox :)

Mark said...

Ah...maybe there's a typo in there or something. I do know that I used this approach and code (which I originally got from the Rotary site) successfully on another SharePoint publishing site that I was working on.

As an aside, take a look at this thread (http://social.msdn.microsoft.com/Forums/en-US/sharepointecm/thread/87574c13-91d6-48fe-8346-01e77e0094b3/). Someone wrote a general-purpose routine that doesn't require the server-generated id to be hardwired into the script.

Anyway...glad you got everything working correctly!