Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript从URL读取变量值_Javascript - Fatal编程技术网

javascript从URL读取变量值

javascript从URL读取变量值,javascript,Javascript,当我将数据从一个页面发布到另一个页面时,我如何将其放入dom中 假设URL为www.test.com/?xxx=1234&a=122 如何在DOM中获得xxx和a值?以下是一个基本教程,介绍如何满足您的要求: 基本上,您将url中的位置字符串拆分为一个数组,在?签字 以下是该页面中执行此操作的代码: // Read a page's GET URL variables and return them as an associative array. function getUrlVars()

当我将数据从一个页面发布到另一个页面时,我如何将其放入dom中

假设URL为www.test.com/?xxx=1234&a=122


如何在DOM中获得xxxa值?

以下是一个基本教程,介绍如何满足您的要求:

基本上,您将url中的位置字符串拆分为一个数组,在?签字

以下是该页面中执行此操作的代码:

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash; 
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); // cut the URL string down to just everything after the ?, split this into an array with information like this: array [0] = "var=xxx"; array [1] = "var2=yyy";

   //loop through each item in that array
    for(var i = 0; i < hashes.length; i++)
    {
        //split the item at the "="
        hash = hashes[i].split('=');

        //put the value name of the first variable into the "vars" array
        vars.push(hash[0]);

        //assign the value to the variable name, now you can access it like this:
        // alert(vars["var1"]); //alerts the value of the var1 variable from the url string
        vars[hash[0]] = hash[1];
    }
    return vars;
}
//读取页面的GET URL变量并将其作为关联数组返回。
函数getUrlVars()
{
var vars=[],散列;
var hashes=window.location.href.slice(window.location.href.indexOf('?'))+1.split('&');//将URL字符串切割为?)后面的所有内容,将其拆分为一个包含以下信息的数组:数组[0]=“var=xxx”;数组[1]=“var2=yyy”;
//循环遍历该数组中的每个项
for(var i=0;i

我添加了一些注释来解释它

只需使用javascript的
window.location.search
属性(更多详细信息)


现在,您可以使用诸如
substring
split
之类的字符串操作函数来获取所需的文本,该文本可“在DOM内部”使用。

URL中的参数可作为对象的搜索属性使用。将其转换为易于访问的对象并不太难:

function getURLValues() {

  var search = window.location.search.replace(/^\?/,'').replace(/\+/g,' ');
  var values = {};

  if (search.length) {
    var part, parts = search.split('&');

    for (var i=0, iLen=parts.length; i<iLen; i++ ) {
      part = parts[i].split('=');
      values[part[0]] = window.decodeURIComponent(part[1]);
    }
  }
  return values;
}
函数getURLValues(){ var search=window.location.search.replace(/^\?/,'').replace(/\+/g''); var值={}; if(search.length){ var part,parts=search.split('&');
对于(var i=0,iLen=parts.length;i:如何在FaceBook中获取iframe URL?您能进一步解释一下您的意思吗?这不是一个很好的教程,例如,它不会解码值或将“+”替换为“”。我根本看不出jQuery在这方面有什么帮助。如果您通读一遍,他最终只是将其作为jQuery的一个小扩展。但不,jQuery没有一点帮助也没有。我只是想展示一种基本的方法,我以前使用过这种方法,因为我从来不用担心+s。在对象可以使用的地方使用数组也被认为是不好的风格。对于对象,不需要使用push,只需直接指定属性(就像数组一样)。我看不到将值分配给数组的数字成员有任何意义。可能存在重复的
function getURLValues() {

  var search = window.location.search.replace(/^\?/,'').replace(/\+/g,' ');
  var values = {};

  if (search.length) {
    var part, parts = search.split('&');

    for (var i=0, iLen=parts.length; i<iLen; i++ ) {
      part = parts[i].split('=');
      values[part[0]] = window.decodeURIComponent(part[1]);
    }
  }
  return values;
}