mount url to open new tab with parameters passed by onclick

1

Good evening, How to make a function to handle past parameters in onclick to mount a url and open it in new tab? example

<button onclick="javascript:parent.funcao1('TESTE','ABCD', 'eydub21lJzonam9hbycsJ2lkYWRlJzogJzI5J30=');">TESTE</button>

Some of the data I need are in base64 in the parameter: "eydub21lJzonam9hbycsJ2lkYWRlJzogJzI5J30=" which are {'name': 'joao', 'age': '29'}

example url to mount: www.teste.com/search.php?name='joao'&ity;'29'&segmento='ABCD '

The mounted url can be opened in an iframe, new div, or new tab. Note: I can not change anything on the button because it is pulled from a database in this format. Thank you in advance.

    
asked by anonymous 10.04.2018 / 02:07

2 answers

1

Using the .atob() method to decode base64, you can convert the result to JSON object and get the data nome and idade .

Receiving the parameters in the function, you can mount a URL and open it in a new window with window.open :

function funcao1(dom,seg,par){
   var par = atob(par).replace(/'/g,"\""); // substituo aspas simples por aspas duplas para o JSON
   par = JSON.parse(par); // crio o JSON
   par = "nome="+par.nome+"&idade="+par.idade; // monto a variável par com o nome e idade
   var url_ = "http://www."+dom.toLowerCase()+".com/pesquisa.php?"+par+"&segmento="+seg;
   window.open(url_,"_blank","..."); // abre em nova janela
}

As the snippet here does not open popup, the result of the url_ variable in the above function will be:

http://www.teste.com/pesquisa.php?nome=joao&idade=29&segmento=ABCD
  

Note: where you have "..." in window.open , you can set the window properties ( see options here ).

    
10.04.2018 / 03:12
0

is there any way to get the parameters being the link in an external iframe? on the same server does exactly what I need, already working with the help of dvd.

function looks like this:

         function funcao1(dom,seg,par){
        var converter = atob(par); // decodifica base64
        var decodif_url = decodeURIComponent(converter); // decodifica uri
        var parx = JSON.parse(decodif_url); 
        var parx = parx.nome; 
        var parx = btoa(parx); // recolocar apenas nome em base64 para URL
        var url_ = "http://www."+dom.toLowerCase()+".com/pesquisa.php?"+parx+"&segmento="+par;
        window.open(url_,"_blank"); // abre em nova guia
        }

link in the iframe EXTERNAL server looks like this:

        <a href="#" onclick="javascript:parent.funcao1('TESTE','ABCD', 'JTdCJTIybm9tZSUyMiUzQSUyMmpvYW8lMjIlMkMlMjJpZGFkZSUyMiUzQSUyMjM3JTIyJTdE');">
                          <nobr>TESTE</nobr>
                       </a>
    
21.04.2018 / 15:39