简体   繁体   English

如何使用href链接将javascript变量作为参数传递?

[英]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.我有一个 JavaScript 变量,我想将它与 HTML 链接一起传递。

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

I get the value of demo on executing one javascript function & few if-else & for loops.我在执行一个 javascript 函数和几个 if-else 和 for 循环时获得了演示的价值。 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.假设在所有这些操作之后,我得到的最终结果存储在 demo 变量中。 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.我试图将演示变量作为参数传递给 href 链接。 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这是我的 Javascript 函数

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.这个函数有一个parameter ,它被传递到a_php_page.php ,在那里执行一些数据库操作和条件检查,并将相应的结果作为 JSON 从 PHP 页面传回 AJAX 函数。 I parse the JSON & obtain demo variable.我解析 JSON 并获取demo变量。 And a HTML modal is included if I get response from the PHP page.如果我从 PHP 页面得到响应,则包含一个 HTML 模式。 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 .在 HTML 模态中,我有一个指向to_fake_page.php的链接,指向我必须在单击链接时传递 demo 变量的位置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();
   }

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.您必须做的(在完成所有逻辑之后,并且您在变量中拥有所需的值)是更改链接的 href。

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/ http://jsfiddle.net/x8tgktv6/

To get the result I modified my JavaScript code like this:为了得到结果,我修改了我的 JavaScript 代码,如下所示:

<script>
var demo=10;
var urll='to_fake_page.php?demostore='+demo;
</script>

and changed the line并改变了线路

'<a href="to_fake_page.php" id="myLink">On click pass demo</a>' + 

in AJAX function to在 AJAX 函数中

'<a href="'+url+'"' +
'id="myLink">On click pass demo</a>' +

It was simple as that.就这么简单。 Added the variable urll to the DOM.将变量urll添加到 DOM。

try this,尝试这个,

<script>
     var demo = 10;
     window.location.href = "to_fake_page.php?demostore=" + demo;
</script>

you want to set a tag href using this function.您想使用此功能设置标签 href。

<script>    
   var link = document.getElementById('aTagId');
   link.setAttribute('href', 'to_fake_page.php?demostore=demo' + demo);
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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