You can use window.location
combined with encodeURIComponent
Example with window.location
:
<form id="search-form" action="" method="GET">
<input type="text" id="search-box">
<input type="submit" value="buscar">
</form>
<script>
(function() {
var searchForm = document.getElementById("search-form");
var searchBox = document.getElementById("search-box");
searchForm.onsubmit = function () {
window.location = "busca?query=" + encodeURIComponent(searchBox.value);
return false;//Isto previne o redirecionamento "nativo" do form
};
})();
</script>
Example with alert
(to see the result) :
var searchForm = document.getElementById("search-form");
var searchBox = document.getElementById("search-box");
searchForm.onsubmit = function () {
alert(encodeURIComponent(searchBox.value));
return false;//Isto previne o redirecionamento "nativo" do form
};
<form id="search-form" action="" method="GET">
<input type="text" value="a+b+c+d+e+f" id="search-box">
<input type="submit" value="buscar">
</form>
Note that encodeURI
is different from encodeURIComponent