Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass javascript variables as parameters with a href link?

I have a JavaScript variable which I wan to pass along with an HTML link.

<script>
var demo=10;
</script>

I get the value of demo on executing one javascript function & few if-else & for loops. Since that code doesn't make any sense to this question, I haven't included that. Assume after all those operations, the final result that I get is stored in the demo variable. Now I wanna pass this demo variable along with an link.

<a href="to_fake_page.php">On click pass demo</a>

I am trying to pass the demo variable as a parameter to the href link. Is this ever possible???

I know

window.location.href = "to_fake_page.php?demostore=demo";

would do the same.

UPDATE

This is my Javascript function

This function has one parameter & it is passed onto a_php_page.php where some database operations & condition checks are performed & the corresponding result is passed back from the PHP page to the AJAX function as JSON. I parse the JSON & obtain demo variable. And a HTML modal is included if I get response from the PHP page. Inside the HTML modal, I have a link pointing to_fake_page.php to where I have to pass the demo variable on clicking a link On click pass demo.

 function onefunction(parameter)
   {
       if(window.XMLHttpRequest)
       {
           xmlhttp=new XMLHttpRequest();
       }
       else
       {
           xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
       }
       xmlhttp.onreadystatechange=function() //callback fn
       {
           if(xmlhttp.readyState==4 && xmlhttp.status==200)
           {
               // these values are obtained from a php page using AJAX.
               // An array has been passed from the php page after json encoding it.

               var jsonStr = JSON.parse(xmlhttp.responseText);
               var demo=jsonStr.value1;
               document.getElementById('myLink').href += '?demo=' + encodeURIComponent(demo);

               $('<div class="modal fade">' +
                   '<div class="modal-dialog">' +
                   '<div class="modal-content">' +
                   demo+
                   '</div>' +
                   '<div class="modal-footer">' +
                   '<a href="to_fake_page.php" id="myLink">On click pass demo</a>' +
                   '</div>' +
                   '</div>' +
                   '</div>' +
                   '</div>').modal();

           }
       }
       xmlhttp.open("GET","a_php_page.php?from="+parameter,true);
       xmlhttp.send();
   }

1 Answers

what you would have to do (after all your logic is done, and you have the value you want inside the variable) is to change the href of the link.

something like this:

var demo = 123;
document.getElementById('myLink').setAttribute('href', 'somelink.php?demo=' + demo);

also put example code here for you:

http://jsfiddle.net/x8tgktv6/

like image 69
developer82 Avatar answered Sep 04 '25 20:09

developer82



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!