在url中调用变量js

时间:2017-03-03 12:40:03

标签: javascript html url

我想从javascript代码中检索变量,并将它们作为html代码中url的参数传递:

这里是javascript代码

function fparsexml() {

  var param = '{"id":"3753612"}';
  var fun4 = "getJsonradio";
  var radio = false;
  var parser, xmlDoc;
  var radio1;


  //fonction getJsonradio
  $.post("http://192.168.20.1:1111/newone/client.php", {
    fun: fun4,
    param: param
  }, function(result) {
    parser = new DOMParser();
    xmlDoc = parser.parseFromString(result, "text/xml");
    //var id_radio=xmlDoc.getElementsByTagName("id").length;
    var id_radio = xmlDoc.getElementsByTagName("Name");

    for (var i = 1; i < 5; i++) {
      alert((id_radio[i].childNodes[0].nodeValue));
      //document.getElementById("demo4").innerHTML = id_radio[0].childNodes[0].nodeValue;
      var radios = id_radio[0].childNodes[0].nodeValue;

      if (i == 4) {
        var rad = true;
      }

    }
    radio1 = radios

  })

这里是HTML代码

<a href="afiichage.php?radio=radio1">ENVOYER</a>

2 个答案:

答案 0 :(得分:0)

在您的Javascript中引用锚标记并修改href属性。

还解决了语法错误,为您的函数提供了闭括号。

在外面声明radios,否则在radio1 = radios;

中使用时会出现范围问题

<script>

  function fparsexml() {

    var param = '{"id":"3753612"}';
    var fun4 = "getJsonradio";
    var radio = false;
    var parser, xmlDoc;
    var radio1, radios;


    //fonction getJsonradio
    $.post("http://192.168.20.1:1111/newone/client.php", {
        fun: fun4,
        param: param
    }, function(result) {
        parser = new DOMParser();
        xmlDoc = parser.parseFromString(result, "text/xml");
        //var id_radio=xmlDoc.getElementsByTagName("id").length;
        var id_radio = xmlDoc.getElementsByTagName("Name");

        for (var i = 1; i < 5; i++) {
            alert((id_radio[i].childNodes[0].nodeValue));
            //document.getElementById("demo4").innerHTML = id_radio[0].childNodes[0].nodeValue;
            radios = id_radio[0].childNodes[0].nodeValue;

            if (i == 4) {
                var rad = true;
            }

        }
        radio1 = radios;
        var elem = document.getElementById("atag");
        var href = elem.getAttribute("href");
        elem.setAttribute("href", href + "?radio=" + radio1);
    });

}

</script>
<a id="atag" href="afiichage.php>ENVOYER</a>

答案 1 :(得分:0)

你在寻找什么网址?

var cuurentURL = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;

在锚

document.getElementById("myAnchor").href = "http://www.foobar.com/";