(HIDE)网址中的参数,然后粘贴到VALUE中

时间:2019-02-12 17:00:12

标签: javascript html

此SCRIPT从URL复制参数“ email =”并粘贴到输入值上。 但是,如果URL上没有该参数,脚本将复制整个URL,如果没有“ email”参数,如何设置为不复制?

还有没有办法只隐藏URL上的“ email”参数,而不显示URL上的用户电子邮件吗?

示例:www.mydomain.com/?xpromo=test&email=anselmo@teste.com&date=20190212 我想从网址中隐藏“ anselmo@teste.com”,但仍将电子邮件复制到输入值上。

 <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>

1 个答案:

答案 0 :(得分: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");