简体   繁体   中英

How can add parameters to href url when click on tag a?

I want From home page send parameter id to carts page:

This is current code, but it not woking:

<a  href="carts" onclick="addParam();">detail</a>
<script>

     function addParam()
     {
         var url = window.location.href;
         var listId = localStorage.getItem('IDs', '');          
         url=url+ "?id=" +listId;
         window.location.href = url;
     }
</script>

How can add parameters to href url when click on tag a?

Either just override it by grabbing the element's (here done by passing this to the function) href

<a  href="carts" onclick="addParam(this);">detail</a>
<script>

     function addParam(el)
     {
         var listId = localStorage.getItem('IDs', '');          
         window.location.href = el.href + "?id=" +listId;
     }
</script>

Or append it to the element's href

<a  href="carts" onclick="addParam(this);">detail</a>
<script>

     function addParam(el)
     {
         var listId = localStorage.getItem('IDs', '');          
         el.href += "?id=" +listId;
     }
</script>

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