将<script>中的字符串插入<a href="">

Pra*_*ani 1 html javascript href

我有以下脚本:

<script>var b=document;var c=varName;b.write(c);</script>
Run Code Online (Sandbox Code Playgroud)

我想将<script>标记中的值传递name给链接中的URL参数,而不是*这里:

`<a href="whatsapp://send?text='Hello This is My Demo Site. Visit http://example.com/ex.html?name=ABC*'"> WhatsApp </a>`
Run Code Online (Sandbox Code Playgroud)

我的尝试导致获取整个脚本字符串:

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

Lit*_*nti 5

不,您不能在HTML属性中插入脚本.您必须首先编写完整的HTML:

<a id="caption" href="whatsapp://send?text=Hello This is My Demo Site. Visit http://example.com/ex.html">link text</a>
Run Code Online (Sandbox Code Playgroud)

...然后,您可以从javascript修改它:

<script>
    var a = document.getElementById("caption");
    var question = "%3F";
    var equal = "%3D";
    a.href += question + "name" + equal + "ABC" + lusername;
</script>
Run Code Online (Sandbox Code Playgroud)

请注意,我故意逃脱"?" 和=""形成参数时,因为我知道它们是http://www.example.comURL的参数而不是whatsapp://sendURL的参数.