简体   繁体   中英

How do I add a function and href to javascript single onclick function?

I want to add a javascript variable value next to a URL. I'll explain with the code below.

First I want a name in the URL, I used the below code to do that,

<script type="text/javascript">
  const queryString = window.location.search;
  const urlParams = new URLSearchParams(queryString);
  var name = urlParams.get('name');
</script>

And then I want to preview the variable's value, like the following:

<script>
   document.write(name);
</script>

Then I want to get the user name as input. for that, I used a prompt.

<script type="text/javascript" language="Javascript">
  function getYourName() {
    name=prompt("Enter you name here");
  }  
</script>

Then I want to share a web URL with the user name as 'get' method,

<a target="blank" href="#" onclick="getYourName(); this.href='whatsapp://send?text=Click here to see magic_ ->https://*sample*.com'+'?name='+**name**;"  data-action="share/whatsapp/share">Share</a>

But It's not working! I want to generate the link as

> Click here to see magic_ -> https://sample.com?name=USER_ENTERED_NAME

How can I integrate the value of the variable into the link?

You can insert the link inside your function getYourName()

function getYourName() {
    const name=prompt("Enter you name here");
    location.href = "https://sample.com?name="+name;
}  

You have to cancel your href attribute inside onclick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM