Javascript 将URL复制到“后”/&引用;并粘贴到按钮上
这个脚本已在运行,但我想知道是否可以只复制“/”(斜杠)后面的“参数” 我的营销活动在“/”后面添加了不同的参数,例如:XV-MEL-OM-REVHL-X-X-POP-X-X 此表单是一个弹出窗口,我需要在“/”之后复制“隐藏”输入参数,以了解用户来自何处 脚本:Javascript 将URL复制到“后”/&引用;并粘贴到按钮上,javascript,html,Javascript,Html,这个脚本已在运行,但我想知道是否可以只复制“/”(斜杠)后面的“参数” 我的营销活动在“/”后面添加了不同的参数,例如:XV-MEL-OM-REVHL-X-X-POP-X-X 此表单是一个弹出窗口,我需要在“/”之后复制“隐藏”输入参数,以了解用户来自何处 脚本: <script type="text/javascript"> function Copy() { var Url = document.getElementById("paste
<script type="text/javascript">
function Copy()
{
var Url = document.getElementById("paste-box");
Url.value = window.location.href;
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>
函数副本()
{
var Url=document.getElementById(“粘贴框”);
Url.value=window.location.href;
Url.focus();
Url.select();
文件。执行命令(“副本”);
}
HTML:
<script type="text/javascript">
function Copy()
{
var Url = document.getElementById("paste-box");
Url.value = window.location.href;
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>
我的网址是:
它只复制/teste lp选项/
顺便说一句,如果“/”后面没有参数,我可以添加参数“XV-MEL-OM-REVHL-X-X-POP-X-X”
Hasta Dhana解决方案正在运行!但是我需要添加参数“XV-MEL-OM-REVHL-X-X-POP-X-X”,如果在“/”后面没有
我认为您正在查找的是的
location.pathname
属性
示例代码:
function Copy()
{
var Url = document.getElementById("paste-box");
Url.value = window.location.pathname;
Url.focus();
Url.select();
document.execCommand("Copy");
}
编辑:
<script type="text/javascript">
function Copy()
{
var Url = document.getElementById("paste-box");
Url.value = window.location.href;
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>
要添加默认参数,可以使用如下正则表达式测试位置:
function Copy()
{
var Url = document.getElementById("paste-box");
Url.value = window.location.pathname + (/\/$/.test(location.pathname) ? "XV-MEL-OM-REVHL-X-X-POP-X-X" : "");
Url.focus();
Url.select();
document.execCommand("Copy");
}
您可以使用
window.location.pathname
而不是window.location.href
仅获取URL的路径部分。如果您需要/
之后的所有内容(如获取参数和散列),您可以获取URL并删除源代码部分:
window.location.href // http://example.com/path?get=value#bang
window.location.origin // http://example.com
window.location.href.slice(window.location.origin.length) // /path?get=value#bang
如果要插入整个URL,但只复制其中的一部分,可以查看
selectionStart
和selectionEnd
属性。这里有一个很好的注释:。您可以使用lastIndexOf()
函数来定位URL中/
字符的最后一部分:
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");
}
我的URL是:它只复制/teste lp选项/I不可能在“/”之后添加参数“XV-MEL-OM-REVHL-X-X-POP-X-X”?它不工作!我的URL是:它只复制了/teste lp选项/I't可能添加参数“XV-MEL-OM-REVHL-X-X-POP-X-X”如果在“/”之后什么都没有?我在答案中添加了一个带有正则表达式的测试,可以吗?我的URL是:它只复制了/teste lp选项/I试图适应下面的情况,但也使用了/teste lp选项/I;函数Copy(){var Url=document.getElementById(“粘贴框”);Url.value=window.location.href.substr(window.location.href.lastIndexOf('/')+1)+(/\/$/.test(location.pathname)“>XV-MEL-OM-REVHL-X-X-POP-X-X:”);Url.focus();Url.select();document.execCommand(“复制”);}正在工作,但如果在“/”之后没有任何内容这是个错误。如果“/”后面没有参数,则可以添加参数“XV-MEL-OM-REVHL-X-X-POP-X-X”?