Read URL and display variable and value on the Javascript page

Asked

Viewed 431 times

0

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

I mean, that would be it:

Example of link: 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.

1 answer

0


Dear user62799,

A quick search right here on stackoverflow already enables us to find a number of answers to that 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;

}

Forms of use:

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

Or in a more performative way:

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

Source: https://www.creativejuiz.fr/blog/en/javascript-en/read-url-get-parameters-with-javascript

Browser other questions tagged

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