I have a javascript function being called by an input type=”image” tag that generates a url and assigns it to window.location.
The problem is that unless I have a return false out of the function AND I actually break into the code and step to the return, it won’t work.
So literally, if I let the code run, it refreshes the page but doesn’t redirect to the new url (url has been verified correct). But if I put a breakpoint in the javascript at window.location = ‘url’, and step to the “return false;” statement, it will work.
In firebug in NET, when it doesn’t redirect, it shows the GET for that call spinning and never completing.
<input type="image" runat="server" id="btnFind" src="images/search.png" align="top" name="Go" onclick="ValidateAndRedirectSearchResults()"/>
I am setting runat to server because I am setting the src in the code behind as well as the
CausesValidation property to false.
Any suggestions?
function ValidateAndRedirectSearchResults() {
var text = document.getElementById('<%=someTextBox.ClientID %>').value;
var url;
if (ValidateSearchBoxContent(text)) {
var criteria = ConvertStringToAscii(text);
var tempUrl = document.getElementById('<%=tempUrl.ClientID %>').value;
url = tempUrl + encodeURIComponent(criteria)
//if I break here and step to return statement it works
//I have tried window.location.href = url as well
window.location = url;
}
return false;
}
Should be
onclick="return ValidateAndRedirectSearchResults()"although this is a horrible way to attach event handlers. JavaScript belongs in.jsfiles in the same way that CSS belongs in.cssfiles