当用户使用特定参数点击URL时,更新iframe表单操作URL

时间:2017-02-21 11:04:50

标签: javascript jquery html iframe

这是代码示例

网页网址为:http://example1.com?id=1

<html>
  <body>
    <h1>main page</h1>
    <iframe src="http://example2.com">
      <form id="test" action="http://example3.com?id=1">
        ...
      </form>
    </iframe>
  </body>
</html>

现在我的问题是当用户点击具有不同参数值的页面URL时如何更新iframe表单操作URL。(例如,如果用户点击带参数id = 2的URL,则表单操作URL应更改为id = 2,即行动=&#34; HTTP://example3.com ID = 2&#34 ;,

如何实现这一目标?

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下代码

在iframe中添加以下代码。并在准备好文件时调用它。

  function getQueries () {
  // This function return value is assigned to QueryString!
  var query_string = {};
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
        // If first entry with this name
    if (typeof query_string[pair[0]] === "undefined") {
      query_string[pair[0]] = decodeURIComponent(pair[1]);
        // If second entry with this name
    } else if (typeof query_string[pair[0]] === "string") {
      var arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
      query_string[pair[0]] = arr;
        // If third or later entry with this name
    } else {
      query_string[pair[0]].push(decodeURIComponent(pair[1]));
    }
  }
  return query_string;
};

这将返回包含在url中传递的参数的对象。

e.g。

$(document).ready(function(){
    var _curQueries = getQueries();
    if(_curQueries["id"] !== undefined)
    {
        var myForm = document.getElementById('test');
        myForm.action = 'http://example3.com?id='+_curQueries["id"];
    }

    /*
    var _params = "";
    var _counter = 0, _length = _curQueries.length;
    for(var i in _curQueries)
    {
        _params += i+"="+_curQueries[i];
        _counter++;
        if(_counter < _length)
        {
          _params += "&";
        }
    }
    var myForm = document.getElementById('test');
    myForm.action = 'http://example3.com?'+_param;
    */
});
相关问题