使用JavaScript获取URL数据(将其拆分为php $ _GET)

时间:2012-10-17 07:21:40

标签: javascript url iframe split window.location

我在Stack Overflow找到了这个脚本:

window.params = function(){
    var params = {};
    var param_array = window.location.href.split('?')[1].split('&');
    for(var i in param_array){
        x = param_array[i].split('=');
        params[x[0]] = x[1];
    }
    return params;
}();

这会将URL拆分为数据,就像PHP与$_GET一样。

我有另一个功能,它使用它并刷新iframe。我想从URL获取数据,如果存在其中一些数据,则添加另一个数据。 Firebug告诉我,search未定义,但为什么?

function RefreshIFrames(MyParameter) {
    var cat = window.params.cat;
    var category = window.params.category;
    var search = window.params.search;

    if (search.length>0 && category.length>0){
        window.location.href="http://siriusradio.hu/kiskunfelegyhaza/video/index.php?search="+search+"&category="+category+"&rendez="+MyParameter;
    }

    if (cat.length>0){
        window.location.href="http://siriusradio.hu/kiskunfelegyhaza/video/index.php?cat="+cat+"&rendez="+MyParameter;
    }

    if (cat.length==0 && category.length==0 && search.length==0){
        window.location.href="http://siriusradio.hu/kiskunfelegyhaza/video/index.php?rendez="+MyParameter;
    }
    alert(window.location);
}

1 个答案:

答案 0 :(得分:0)

如果要添加rendez或更改现有的rendez,请执行此操作 - 我假设URL实际上以http://siriusradio.hu/kiskunfelegyhaza/video/index.php开头,因此无需创建它。如果您需要的网址与您使用的网址不同,请与我们联系

参数片段无法正常工作(不应在普通数组中使用)

这是经过测试的代码

function getParams(passedloc){
  var params = {}, loc = passedloc || document.URL;
  loc = loc.split('?')[1];
  if (loc) {
    var param_array = loc.split('&');
    for(var x,i=0,n=param_array.length;i<n; i++) {
      x = param_array[i].split('=');
      params[x[0]] = x[1];
    }
  }
  return params;
};

function RefreshIFrames(MyParameter,passedloc) { // if second parm is specified it will take that 
  var loc = passedloc || document.URL; // 
  window.param = getParams(loc); 
  loc = loc.split("?")[0]+"?"; // will work with our without the ? in the URL
  for (var parm in window.param) {
    if (parm != "rendez") loc += parm +"="+ window.param[parm]+"&";
  }
  // here we have a URL without rendez but with all other parameters if there
  // the URL will have a trailing ? or & depending on existence of parameters
  loc += "rendez="+MyParameter;
  window.console && console.log(loc)
  // the next statement will change the URL
  // change window.location to window.frames[0].location to change an iFrame
  window.location = loc; 
}

// the second parameter is only if you want to change the URL of the page you are in
RefreshIFrames("rendez1","http://siriusradio.hu/kiskunfelegyhaza/video/index.php?cat=cat1&search=search1");
RefreshIFrames("rendez2","http://siriusradio.hu/kiskunfelegyhaza/video/index.php?search=search2");
RefreshIFrames("rendez3","http://siriusradio.hu/kiskunfelegyhaza/video/index.php?rendez=xxx&search=search2");
RefreshIFrames("rendez4","http://siriusradio.hu/kiskunfelegyhaza/video/index.php");

// here is how I expect you want to call it    
RefreshIFrames("rendez5"​); // will add or change rendez=... in the url of the current page