Read URL and display variable and value in the Javascript page

0

I have a link mounted with variants of an email marketing, clicking on the link would like to pass some information on it and display them on the other page.

That is, this would be it:

Link example: www.siteexemplo.com.br?nome=gustavo?idade=12

By clicking this link, the next page will read the URL and display the data:

Dados
Nome: Gustavo
Idade: 12

If possible, in JavaScript.

    
asked by anonymous 24.03.2017 / 03:20

1 answer

0

Dear user62799,

A quick search right here on stackoverflow already enables us to find a number of answers to this question. However, I particularly prefer the following approach:

function $_GET(param) {
var vars = {};
window.location.href.replace( location.hash, '' ).replace( 
    /[?&]+([^=&]+)=?([^&]*)?/gi,
    function( m, key, value ) {
        vars[key] = value !== undefined ? value : '';
    }
);

if ( param ) {
    return vars[param] ? vars[param] : null;    
}
return vars;

}

Ways to use:

var  nome = $_GET('nome'),
var idade = $_GET('idade'),

Or more performatively:

var $_GET = $_GET(),
     nome = $_GET['nome'],
    idade = $_GET['idade'];

Source: link

    
24.03.2017 / 04:06