How to interpret "+" as space in the url?

Asked

Viewed 1,005 times

2

Well, I have the search page on my site, and when the initial search form, you type "stack overflow" as a search, and the url gets stack+overflow instead of stack%20overflow. Has to interpret "+" as "space" and + %2B as "+" using javascript?

  • You already have the answer, just use the function encodeURIComponent(); in the Submit action of your form; http://jsfiddle.net/Wagner/xaheddrr/

  • Confirming if I understand: you want to read the value of the URL and convert the + back to spaces?

  • 1

    It’s strange the result you want to get, but I believe using something like : //exemplo de url: "www.seudominio.com.br/busca.php?stack+overflow";
var parametroDeBusca = location.search.substring(1);
parametroDeBusca.replace('+', ' '); work

  • 1

    If what he wants is at "read" time, then the problem is not a question of javascript, but of server-side (server answer). Gustavo please rephrase the question so we can be sure what your need.

1 answer

1


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 be able to view 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

  • There is no way to use window.Location, as I have a form action change script to change the query destination.

  • So if there is some "dependency" on your code you should have added the entire code in the question, because if not the issue loses focus, since you did not specify this. Edit your question and add the entire code. Thank you

  • But is there a way in the search page to convert "+" into space? Follow the google example: http://google.com/search?q=stack+overflow . It converts the + into space.

  • But when encoding +, it turns %2b, not %20, space equivalent.

  • with encodeURI yes + is a space with encodeURIComponent, the + is considered more than a sign. Try using the example I posted in a new html without your code. Read the documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent to understand the differences

  • Please "downvoter" specify the reason for the negative vote, so I can improve the answer.

  • I don’t know who it was or why, but there are people who vote counts when they don’t think the question is clear and the answer is based on an assumption (in this case, that he wants to encode the querystring before posting the form, and not reverse the encoding). But it’s just a kick, only the downvoter himself could confirm the motive.

  • @bfavaretto thank you, yes I imagine it is really something of the kind, in all communities "stackexchange" there is such a situation, maybe it is fear to create some "feud", I personally see the negative vote as something useful and do not take offense, as long as I’m able to understand where I can improve. Grateful

  • I am sorry, but it was not I who voted 'no', on the contrary I voted 'yes' and even accepted your reply.

  • @Gustavobarbosa thank you, I did not say it was you, I just wanted "downvoter" said what my fault was so I could improve the issue. Changing the subject, did you really solve your problem? Hugs

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.