views:

271

answers:

2

I'm using an asp textbox and a search button. In Safari if I click the search button i get redirected to the search results page using javascript window.location.href. But strangely the same javascript will not redirect to the page if I press return in the textbox.

Using the alert function I can see that window.location.href has the the correct url and the location bar at the top changes from the search page(default.aspx) to the search results url however when I click OK to the alert box the url at the top reverts back to the default.aspx page. It works on ie7/8/firefox/chrome but not safari. Here is my javascript,cs and aspx code:

function submitSearchOnEnter(e) {

            var CodeForEnter = 13;
            var codeEnteredByUser;

            if (!e) var e = window.event;
            if (e.keyCode) codeEnteredByUser = e.keyCode;
            else if (e.which) codeEnteredByUser = e.which;

            if (codeEnteredByUser == CodeForEnter)
                RedirectToSearchPage();
        }

        function RedirectToSearchPage() {

            var searchText = $get('<%=txtHeaderSearch.ClientID%>').value

            if (searchText.length) {

                window.location.href = "Search.aspx?searchString=" + searchText;
            }
        }

protected void Page_Load(object sender, EventArgs e)
{
    txtHeaderSearch.Attributes.Add("onkeypress", "submitSearchOnEnter(event)");
}

<asp:Panel ID="pnlSearch" runat="server" DefaultButton="lnkSearch">
                <asp:TextBox ID="txtHeaderSearch" runat="server" CssClass="searchBox"></asp:TextBox>
                <asp:LinkButton ID="lnkSearch" OnClientClick="RedirectToSearchPage(); return false;"
                    CausesValidation="false" runat="server" CssClass="searchButton">
            SEARCH
                </asp:LinkButton>
            </asp:Panel>

I've tried return false; which doesn't allow me to enter any characters in the search box. I've spent ages online trying to find a solution. Maybe it has something to do with setTimeout or setTimeInterval but it didn't work unless i did it wrong.

+1  A: 

In Safari, the 'onkeypress' event is only fired if the key press results in a character being added to an HTML element. I'm not sure that this will fire at all when you press Enter in a one-line text box. See a partial explanation of this behavior here.

As Tim Down says, you probably want onkeydown instead.

Syntactic
I tried Tim Down's suggestion but it didn't work on Safari and also Chrome. onkeypress works in chrome. If you read my comment in Tim Downs answer it explains me using the alert box to read the window.location.href which was http://localhost (newline) http://localhost:55897/.../Default.aspx BUT in the url at the top it reads the correct page and search string????
insanepaul
+1  A: 

Use keydown instead of keypress and you should be fine.

protected void Page_Load(object sender, EventArgs e)
{
    txtHeaderSearch.Attributes.Add("onkeydown", "submitSearchOnEnter(event)");
}
Tim Down
I've just had the chance to try your suggestion but it didn't work and it doesn't work on chrome. The weird thing is that when I put an alert box after the last line to see what the url string will be ...window.location.href = "Search.aspx?searchString=" + searchText; alert(window.location.href);... the url at the top reads Search.aspx?searchString=doctor but the alert message reads Default.aspx.
insanepaul