Passing an URL parameter to a href link using Javascript

前端 未结 2 508
借酒劲吻你
借酒劲吻你 2021-01-06 11:53

I have the following code working to pass a URL parameter to a form tag:



        
相关标签:
2条回答
  • 2021-01-06 12:15

    Change the function onload to this:

    function onLoad() {
       var value = getQueryVariable("ID");
       var e = document.getElementById('your-field');
       e.value = value;
    
       var url = "http://www.mysite.com?source=" + value;
       var element = document.getElementById('YOUR_<A>_ELEMENT_ID');
       element.setAttribute("href",url)
    }
    

    I'm using the piece of code that Joao Almeida suggested so his example using jQuery works good too.

    Good Luck!

    0 讨论(0)
  • 2021-01-06 12:30

    You should give an id to you link, like this:

    <a id="YOUR_ID" href="#" >
    

    And then you have two ways to solve the problem, use pure Javascript or use jQuery:

    IF you use jquery you can use your onLoad function and inside inject the following:

    var url = "http://www.mysite.com?source=" + value;
    $("#YOUR_ID").attr("href",url)
    

    OR using pure javascript:

    var url = "http://www.mysite.com?source=" + value;
    var element = document.getElementById('YOUR_ID');
    element.setAttribute("href",url)
    
    0 讨论(0)
提交回复
热议问题