3

I have the following script:

<script>var b=document;var c=varName;b.write(c);</script>

and I want to pass the value from the <script> tag to the name URL parameter in a link, in place of the * here:

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

My attempt resulted in getting the whole script string:

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

2
  • Apologies, but this is completely unclear. Perhaps you could have a friend or colleague help with the English. Commented Dec 15, 2015 at 15:57
  • @T.J.Crowder I have edited. Commented Dec 15, 2015 at 16:05

2 Answers 2

7

No, you cannot insert scripts within an HTML attribute. You must compose the complete HTML first:

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

... And then, you can modify it from javascript:

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

Note that I did intentionally escape "?" and "=" when forming the parameters, because I understand they are arguments for the http://www.example.com URL and not for the whatsapp://send URL.

Sign up to request clarification or add additional context in comments.

2 Comments

You are really awesome :) Nice Understanding. I am sorry for my bad English but you got me. Accepted :)
@PratikButani Thanks. :-)
0

If the value of varName is known when the page loads, and is set in a script tag earlier in the HTML than where you want the link, then you can just output the entire link with script:

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

If varName is dynamic, then you'll want to do something like Little Santi showed you each time you change it. See also his/her point about encoding the ? and = on the example.com link text, which I've applied above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.