将 <script> 中的字符串插入到 <a href="">
Insert string from <script> into to <a href="">
我有以下脚本:
<script>var b=document;var c=varName;b.write(c);</script>
我想将 <script>
标签的值传递给 link 中的 name
URL 参数,代替 *
这里:
`<a href="whatsapp://send?text='Hello This is My Demo Site. Visit http://example.com/ex.html?name=ABC*'"> WhatsApp </a>`
我的尝试得到了整个脚本字符串:
<a href="whatsapp://send?text='Hello This is My Demo Site. Visit http://example.com/ex.html?name=ABC<script>var b=document;var c=varName;b.write(c);</script>'"> WhatsApp </a>
不,您不能在 HTML 属性中插入脚本。你必须先写完整的 HTML:
<a id="caption" href="whatsapp://send?text=Hello This is My Demo Site. Visit http://example.com/ex.html">link text</a>
... 然后,您可以从 javascript:
修改它
<script>
var a = document.getElementById("caption");
var question = "%3F";
var equal = "%3D";
a.href += question + "name" + equal + "ABC" + lusername;
</script>
请注意,我确实故意转义了“?”和“=”形成参数时,因为我知道它们是 http://www.example.com
URL 的参数而不是 whatsapp://send
URL.
的参数
如果 varName
的值在页面加载时已知,并且在 HTML 中的 script
标记中设置的时间早于您想要 link 的位置, 然后你可以用脚本输出整个 link:
<script>
document.write(
'<a href="whatsapp://send?text=\'Hello This is My Demo Site. Visit http://example.com/ex.html%3Fname%3DABC' +
varName + '\'"> WhatsApp </a>'
);
</script>
如果 varName
是动态的,那么您每次更改它时都需要执行类似 的操作。另请参阅 his/her 关于对 example.com
link 文本中的 ?
和 =
进行编码的要点,我在上面应用了这一点。
我有以下脚本:
<script>var b=document;var c=varName;b.write(c);</script>
我想将 <script>
标签的值传递给 link 中的 name
URL 参数,代替 *
这里:
`<a href="whatsapp://send?text='Hello This is My Demo Site. Visit http://example.com/ex.html?name=ABC*'"> WhatsApp </a>`
我的尝试得到了整个脚本字符串:
<a href="whatsapp://send?text='Hello This is My Demo Site. Visit http://example.com/ex.html?name=ABC<script>var b=document;var c=varName;b.write(c);</script>'"> WhatsApp </a>
不,您不能在 HTML 属性中插入脚本。你必须先写完整的 HTML:
<a id="caption" href="whatsapp://send?text=Hello This is My Demo Site. Visit http://example.com/ex.html">link text</a>
... 然后,您可以从 javascript:
修改它<script>
var a = document.getElementById("caption");
var question = "%3F";
var equal = "%3D";
a.href += question + "name" + equal + "ABC" + lusername;
</script>
请注意,我确实故意转义了“?”和“=”形成参数时,因为我知道它们是 http://www.example.com
URL 的参数而不是 whatsapp://send
URL.
如果 varName
的值在页面加载时已知,并且在 HTML 中的 script
标记中设置的时间早于您想要 link 的位置, 然后你可以用脚本输出整个 link:
<script>
document.write(
'<a href="whatsapp://send?text=\'Hello This is My Demo Site. Visit http://example.com/ex.html%3Fname%3DABC' +
varName + '\'"> WhatsApp </a>'
);
</script>
如果 varName
是动态的,那么您每次更改它时都需要执行类似 example.com
link 文本中的 ?
和 =
进行编码的要点,我在上面应用了这一点。