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();
}