复制URL并粘贴在按钮上,如果“ /”后没有任何内容,则添加默认参数

时间:2018-10-25 16:09:26

标签: javascript html

下面的脚本可以工作,但是如果“ /”后没有参数,则不起作用

是否可以在“ /”之后添加“ default”参数?

  

默认参数:“ XV-MEL-OM-REVHL-X-X-POP-X-X

     

示例:www.domain.com/test-new/ 默认参数

我的网址是:http://lp.inversapub.com/test-new/

function Copy() 
{
    var Url = document.getElementById("paste-box");
    Url.value = window.location.href.substr(window.location.href.lastIndexOf('/') + 1);
    Url.focus();
    Url.select();  
    document.execCommand("Copy");
}
<form action="https://ws.inversapub.com/subscribe" method="POST" target="hiddenFrame">
    
    <input name="emailAddress" type="email" required="" oninput="this.setCustomValidity('')" oninvalid="this.setCustomValidity('Preencha um endereço de e-mail válido.')" placeholder="Coloque seu e-mail aqui" class="input-email-popup">
    
    <input type="submit" value="QUERO DOBRAR MEU DINHEIRO AINDA ESTE ANO" onclick="Copy();" class="btn-submit-popup om-trigger-conversion">
    <input type="hidden" name="sourceId" id="paste-box" value="XV-MEL-OM-REVHL-X-X-POP-X-X">
    
    <input name="listCode" type="hidden" value="inv_hotlist_revhl"/>
    
    <input name="redirect" type="hidden"  value="#" />
    
    <input name="email_page" type="hidden" value="inv_welcome_revhl"/>
    
</form>

1 个答案:

答案 0 :(得分:0)

如果'/'之后未提供任何内容,则可以尝试将复制功能更新为使用默认值。

function Copy() 
{
    var Url = document.getElementById("paste-box");
  let parameter = window.location.href.substr(window.location.href.lastIndexOf('/') + 1);

    if(parameter) {
      Url.value = parameter
    } else {
      Url.value = "XV-MEL-OM-REVHL-X-X-POP-X-X";
    }
    Url.focus();
    Url.select();  
    document.execCommand("Copy");
}