How to add parameters in the html action?

Asked

Viewed 780 times

1

Now I just asked about the topbar links of my site. Well, now the problem is another: add parameters to a url. Follow the code:

<script type="text/javascript">
function web(){
document.form.action = "http://www.google.com/search?q=";
}
function images(){
document.form.action = "http://www.google.com/search?q=";
}
function videos(){
document.form.action = "http://www.google.com/search?q=";
}
function news(){
document.form.action = "http://www.google.com/search?q=";
}
</script>

The Google search parameter by:

imagens: &tbm=isch, vídeos: &tbm=vid, notícias: &tbm=nws.

How do I add these parameters to the url?

  • Can you explain better what you want to do? and what you’ve tried?

  • I want to add the &tbm=isch parameter to the google search url in my form without affecting it. Ex: http://www.google.com/search?q=qresearch&tbm=isch

2 answers

2

"The order of the parameters does not change the product" :)

The parameter pairs are separated by &, but in most applications, the order in which you present them is irrelevant. So, just use the format below:

<script type="text/javascript">
function web(){
   document.form.action = "http://www.google.com/search?q=";
}
function images(){
   document.form.action = "http://www.google.com/search?tbm=isch&q=";
}
function videos(){
   document.form.action = "http://www.google.com/search?tbm=vid&q=";
}
function news(){
   document.form.action = "http://www.google.com/search?tbm=nws&q=";
}
</script>

Example of link: http://www.google.com/search?tbm=isch&q=M.C.Escher

Note: this only makes sense if you are calling the function by JS, because if you want to take advantage of the fields of a normal form, you should not be sending nor the tbm nor the q in the action, and yes as fields.

  • Now I was curious... Have you seen any Application in which the order of the GET parameters influences something?

  • @Brunoaugusto yes, has a billing company for billets and card in Brazil that had the ability to use string positional parse for both XML and Querystring. Simply change the order of the parameters (or nodes at the same depth), and the trick fails. Of course this is their gambit, if they had followed the specification it would never have happened (but I’ve seen it happen in more than one case).

  • @Brunoaugusto also remembered a company "chipeira" SMS suffering from the same evil in the messaging API. The GET parameters have to be in order. Too bad I can’t say the name here :)

  • @I have seen several systems that use array of parameters. For example, if there is a table with multiple checkboxes: item=1&item=5&item=7. Then this is retrieved from a server-side list that can consider the order of items.

0

You need to add one <input type="hidden" name="tbm"> within your <form>, then you can do it like this:

document.form.tbm.value = "isch";

or

document.form.elements["tbm"].value = "isch";

This way, your code would look like this:

<script type="text/javascript">
function web(){
    document.form.action = "http://www.google.com/search";
}
function images(){
    document.form.action = "http://www.google.com/search";
    document.form.tbm.value = "isch";
}
function videos(){
    document.form.action = "http://www.google.com/search";
    document.form.tbm.value = "vid";
}
function news(){
    document.form.action = "http://www.google.com/search";
    document.form.tbm.value = "nws";
}
</script>

Reference: http://www.w3schools.com/jsref/prop_form_name.asp

  • i like your solution, but it would be cool if you edit the answer and put the OP code adapted to what you are explaining, would greatly value your explanation.

  • Very Obg! It worked! But these functions are called per link, and when we click on them, the search-box loses focus. You can keep it selected?

  • @Gustavobarbosa to focus on one element, use document.getElementById("idDoElemento").focus();.

Browser other questions tagged

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