How to add parameters in action html?

1

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

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

The Google search parameter for:

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

How do I add these parameters to the url?

    
asked by anonymous 04.10.2014 / 19:22

2 answers

2

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

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

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

Link example: link

Note: This only makes sense if you are to call the function by JS, because if you want to take advantage of normal form fields, you should not be sending tbm nor q not action , but rather as fields.

    
04.10.2014 / 20:25
0

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

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

or

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

In this way, your code would look like this:

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

Reference: link

    
04.10.2014 / 19:46