views:

138

answers:

3

Hi, I have a textbox for search and a button that redirects to given value of the textbox. It works well except for firefox 3, which ignores the function completely. Any ideas why and how to fix it? I have already tried window.location instead of location.href, but it again works well in all major browsers but firefox.

My code:

<%=Html.TextBox("search", Html.Encode(ViewData["search"])) %>    
<input type="button" onclick="location.href='<%= Url.Content("~/Authorized/Accounts/0/1/") %>'+search.value" value="Search" />

EDIT

And here is the generated code:

<input id="search" name="search" type="text" value="" /> 
<input type="button" onclick="window.location='/Authorized/Accounts/0/1/'+search.value" value="Search" />
A: 

I'm not sure why your code doesn't work in FF3, but I got it to work by adding the domain to the beginning of the URL and also by removing the leading slash.

So, if it's possible for you to either add the domain or use relative paths in that location, it should work. For example, the generated line might look like this:

<input type="button" onclick="window.location='http://www.example.com/Authorized/Accounts/0/1/'+search.value" value="Search" />

Aaron
I thought of this too, but I wanted to be able to put it on a different domain without having to refactor all apearences. But thanks.
Trimack
+1  A: 

search.value is looking for a JavaScript variable called 'search', not your form field.

You should be using document.getElementById('search').value

Diodeus
In my tests, I found that search.value returned the expected value in Firefox and IE.
Aaron
Your original html worked in FF3.5, but it also produced a clear error message on the console:Warning: Element referenced by ID/NAME in the global scope. Use W3C standard document.getElementById() instead.Source File: file:///tmp/tst.htmlI'd start by fixing the non standard javascript (regardless is it worked in your tests, it still needs cleaning up. It's 2010 and we're still reaping the "benefits" sowed by IE 6 and earlier Netscape browsers that accepted broken html and still produced results producing a generation of designers who through their broken junk was correct.
SuperMagic
+1  A: 
<%=Html.TextBox("search", Html.Encode(ViewData["search"])) %>    
<input type="button" onclick="location.href='<%= Url.Content("~/Authorized/Accounts/0/1/") %>'+document.getElementById('search').value;" value="Search" />

If you're using jQuery:

<%=Html.TextBox("search", Html.Encode(ViewData["search"])) %>    
<input type="button" onclick="location.href='<%= Url.Content("~/Authorized/Accounts/0/1/") %>'+$("#search").val();" value="Search" />
Kordonme