Variable handling in HTML

1

Hello ... The world HTLM / JAVASSCRIPT / PHP is totally new to me. As a beginner, I count on your help to develop a small project. Come on ...

I have an INDEX.HTML page that receives a value via URL. I was able to read this value using a function called CAPTURAPARAMETROURL () that I got in this forum. Now I need to open a page called CADASTRO.HTML when the user clicks REGISTRATION passing this value obtained by the function.

Who can help me?

The INDEX.HTML code is this:

<!DOCTYPE html>
<html lang="pt-br">
<head>
	<title>MEU SITE</title>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
<!--===============================================================================================-->
	<link rel="icon" type="image/png" href="images/icons/favicon.ico"/>
	<link rel="stylesheet" type="text/css" href="css/util.css">
	<link rel="stylesheet" type="text/css" href="css/main.css">
<!--===============================================================================================-->

</head>

<body background="images/bg/bg2.jpg">


	<div class="container-meusite">

		<div class="wrap-meusite" >

			<div class="meusite-form-title" style="background-image: url(images/title.png);">
			</div>

                            <div class="container-meusite-form-btn">

                                <div class="container-meusite-form-btn">
				</div>

                            </div>

                                <center><button class="meusite-form-btn">
                                        <a href="cadastro.html?" style="color: #fff"><span>Inscri&ccedil;&atilde;o</span></a>


                                </button>

                                </br>

                            <button class="meusite-form-btn" >
                                <a href="termo.html" style="color: #fff"><span>
                                                    Termos de uso

                                                 </span></a>

                                </button></center>
                    </br>
	</div>
                </div>

<script type="text/javascript" src="js/jquery.js"></script>

<script>

window.onload = CapturaParametrosUrl();

/* Captura parametros da URL com JS*/
function CapturaParametrosUrl() {
var url = window.location.href; //retorna a url da página

var res = url.split('?'); //tenta localizar o ?

if (res[1] !== undefined) {

var parametros = res[1].split('&'); //tenta localizar os & (pode haver mais de 1)

if (parametros[0] !== undefined) {
parametro0 = parametros[0].split('=');
parametro0 = parametro0[1];
var promocional = parametros[0]
document.getElementById('resultado').innerHTML = parametro0;
}

if (parametros[1] !== undefined) {
parametro1 = parametros[1].split('=');
parametro1 = parametro1[1];
document.getElementById('resultado').innerHTML += ', ' + parametro1;
}

if (parametros[2] !== undefined) {
parametro2 = parametros[2].split('=');
parametro2 = parametro2[1];
document.getElementById('resultado').innerHTML += ', ' + parametro2;
}
}
}
</script>
</body>

</html>
    
asked by anonymous 10.06.2018 / 01:14

1 answer

0

You can go concatenating the parameters and then change the href of the link by adding the parameters.

First, remove the ? from the link, leaving only href="cadastro.html" :

<a href="cadastro.html" style="color: #fff"><span>Inscri&ccedil;&atilde;o</span></a>

Then use the code below (see explanations in the code):

window.onload = CapturaParametrosUrl();

/* Captura parametros da URL com JS*/
function CapturaParametrosUrl() {
   var url = window.location.href; //retorna a url da página

   var res = url.split('?'); //tenta localizar o ?

   var params = ''; // cria uma variável para os parâmetros

   if (res[1] !== undefined) {

      var parametros = res[1].split('&'); //tenta localizar os & (pode haver mais de 1)

      if (parametros[0] !== undefined) {
         parametro0 = parametros[0].split('=');
         parametro0 = parametro0[1];
         var promocional = parametros[0]
         //document.getElementById('resultado').innerHTML = parametro0;

         params += "?param1="+parametro0; // adiciona o 1º parâmetro
      }

      if (parametros[1] !== undefined) {
         parametro1 = parametros[1].split('=');
         parametro1 = parametro1[1];
         //document.getElementById('resultado').innerHTML += ', ' + parametro1;
         params += "&param2="+parametro1; // adiciona o 2º parâmetro
      }

      if (parametros[2] !== undefined) {
         parametro2 = parametros[2].split('=');
         parametro2 = parametro2[1];
         //document.getElementById('resultado').innerHTML += ', ' + parametro2;
         params += "&param3="+parametro2; // adiciona o 3º parâmetro
      }
   }

   if(params != ""){ // se houve parâmetros, atualizo o href do link com eles
      document.querySelector("a[href='cadastro.html']").href = "cadastro.html"+params;
   }
}

In the example above, I used the parameter names as param1 , param2 and param3 . These names you can change with the ones you want, you just change them in the code.

    
10.06.2018 / 02:27