mount url to open new tab with parameters passed by onclick

Asked

Viewed 1,054 times

1

How to do a function to handle parameters passed on onclick to mount a URL and open it in a new tab? Example:

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

part of the data I need are in Base64 in the parameter: eydub21lJzonam9hbycsJ2lkYWRlJzogJzI5J30=" que são {'nome':'joao','idade':'29'}

example URL to mount: www.teste.com/pesquisa.php?nome='joao'&idade='29'&segmento='ABCD'

Mounted URL can be opened on iframe, when new div, or new tab. Note: I cannot change anything on button 'cause it’s pulled from a database in this formatting. Thanks in advance.

2 answers

1


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

Receiving the parameters in the function, you can mount a URL and open 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
}

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

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

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

  • perfectly attended what I needed, thank you

0

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

function this way:

         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 on EXTERNAL server iframe like this:

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

Browser other questions tagged

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