0

This SCRIPT copy the parameter "email=" from URL and paste on a input value. But when that's no parameter on the URL, the scripts copys the entire URL, how can I set to don't copy if doesn't have the "email" parameter ?

Also, is there a way to hide only the "email" parameter on URL, to not show the user email on the URL?

Example: www.mydomain.com/?xpromo=test&email=anselmo@teste.com&date=20190212 I want to hide the "anselmo@teste.com" from the URL but still COPY the email on the input value.

 <script type="text/javascript">
  document.addEventListener("DOMContentLoaded", function(event) {
  var url=window.location.href;
  var email=url.split("email=").pop(-1);
  document.getElementById('email-assinante').value=email;
});
</script>


<form action="https://ws.inversapub.com/subscribe" data-validate="https://lp.inversapub.com/wp/wp-admin/admin-ajax.php" method="POST">
        <input name="emailAddress" type="email" required="" oninput="this.setCustomValidity('')" oninvalid="this.setCustomValidity('Preencha um endereço de e-mail válido.')" placeholder="Coloque aqui seu melhor e-mail" class="form-control input-box" value="" id="email-assinante" >

        <input type="text" class="form-control input-box" name="celular" placeholder="Deixe aqui seu número de celular com DDD" class="telephone" required="" >

        <input type="submit" value="COMEÇAR A RECEBER" class="btn btn-email">

             <input name="sourceId" type="hidden" value="XPROMO" />
          <input name="listCode" type="hidden" value="HOTLIST" />
          <input name="redirect" type="hidden" value="THANK YOU PAGE" />
      <input name="email_page" type="hidden" value /> 
    </form>
Aziz.G
  • 3,599
  • 2
  • 17
  • 35
Anselmo
  • 13
  • 5
  • 2
    The question doesn't appear to be related to java. Please remove the java tag. – Andrew S Feb 12 '19 at 17:02
  • 2
    Get the parameter value and check if it's empty -> https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript/901144#901144 – daddygames Feb 12 '19 at 17:02

1 Answers1

0

function getQueryParam(param) {
  // returns the query parameters portion from the url and gets rid of the ? at position [0]
  var query = window.location.search.substring(1);
  var startPos = query.indexOf(param);
  if (startPos == -1) {
    return false;
  }
  query = query.substring(startPos);

  // checks if the desired param is the last one in the query
  if (query.indexOf('&') > -1) {
    var endPos = (query.indexOf('&', startPos) == -1) ? query.indexOf('&') :
      query.indexOf('&', startPos);
  }

  var keyValue = query.substring(startPos, endPos);
  return keyValue.split('=')[1];
}


/*this will return the email value from your url or any other params*/
const email = getQueryParam("email");
Aziz.G
  • 3,599
  • 2
  • 17
  • 35