0

hi I have script that provides search in multiple sites. But i cannot understand
why at the end we use

return false;

?
whynot we use return true?

<script type="text/javascript">
function dosearch() {
var sf=document.searchform;
for (i=sf.sengines.length-1; i > -1; i--) {
if (sf.sengines[i].checked) {
var submitto = sf.sengines[i].value + escape(sf.searchterms.value);
}
}
window.location.href = submitto;
return false;
}
</script>


<form name="searchform" onSubmit="return dosearch();">
Search:<br />
<input name="sengines" type="radio" value="http://www.google.com/search?q="> Google<br />
<input name="sengines" type="radio" value="http://www.altavista.com/web/results?q="> Alta Vista<br />
<input name="sengines" type="radio" value="http://www.dogpile.com/info.dogpl/search/web/"> Dogpile<br />
<input name="sengines" type="radio" value="http://search.yahoo.com/search?p="> Yahoo!<br />
For: 
<input type="text" name="searchterms">
<input type="submit" name="SearchSubmit" value="Search">
</form>

Thanks for attention

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by azegurb
0

To understand that, you need to realize that your doSearch() has the following: window.location.href = submitto; That line basically will change the browser's url to whatever is in the submitto variable. Essentially that line is emitting a search request using method="get" , since all the search parameters are already included in the submitto variable.

If you were to REMOVE that line and get rid of return false, then you would notice that the same thing happens - meaning, the url changes so that it contains all the search parameters. Since you do NOT have <form method='post'...> , the browser defaults to <form method='get'...> . This is the reason why the url would change and include the search parameters if you were to get rid of return false; and the [ window.location.href = submitto; So, to summarize, if you get rid of return false; , the browser would submit the FORM 's search request using method="get" and by having window.location.href = submitto; it would ALSO submit a search request using method="get" .

Clearly, there is NO need to submit two requests for every search you make. So, by adding return false, the FORM 's get request is cancelled.

0

Thank you very much. but i didnt include get method in that form.
i didnt understand because i didnt use get method there

0

but i didnt include get method in that form.
i didnt understand because i didnt use get method there

But if you do NOT see any method attribute at all, it is expected that as the web developer you would already know that it defaults to GET.

Here's the link to the relevant W3C spec in case you haven't read it:
http://www.w3.org/TR/html4/interact/forms.html#h-17.3

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.