访问直接从JavaScript得到什么?(Access GET directly from Java

2019-08-20 03:40发布

我想我可以使用PHP访问$_GET从JavaScript变量:

<script>
var to = $_GET['to'];
var from = $_GET['from'];
</script>
<script src="realScript" type="text/javascript"></script>

但也许这是更简单。 有没有办法直接从JS做呢?

Answer 1:

看着

window.location.search

它将包含这样的字符串: ?foo=1&bar=2

要从进入的对象,一些分裂是所有你需要做的:

var parts = window.location.search.substr(1).split("&");
var $_GET = {};
for (var i = 0; i < parts.length; i++) {
    var temp = parts[i].split("=");
    $_GET[decodeURIComponent(temp[0])] = decodeURIComponent(temp[1]);
}

alert($_GET['foo']); // 1
alert($_GET.bar);    // 2


Answer 2:

这里的另一个想法:

<script type="text/javascript">

var $_GET = <?php echo json_encode($_GET); ?>;

alert($_GET['some_key']);
// or
alert($_GET.some_key);

</script>


Answer 3:

我想你在想这样的:

<script type="text/javascript">

    var to = "<?= $_GET['to']; ?>";
    var from = "<?= $_GET['from']; ?>";

</script>

...这也只是你的想法的语法修正:)



Answer 4:

我知道这个话题是老了,但我想分享的$ _GET在JavaScript我自己ES6 optomized解决方案。 似乎所有的关于这个问题比较流行的问题,从捐款SO新手锁定,所以在这里它是:

一个内胆

window.$_GET = location.search.substr(1).split("&").reduce((o,i)=>(u=decodeURIComponent,[k,v]=i.split("="),o[u(k)]=v&&u(v),o),{});

我很想你所有链接到MDN文档上array.reduce(), 箭头功能逗号操作符解构分配短cicuit评价 ,但是,唉,另一SO新手限制。

对于像一个URL google.com/webhp?q=foo&hl=en&source=lnt&tbs=qdr%3Aw&sa=X&ved=&biw=12你有一个对象:

$_GET = {
   q: "foo",
   hl: "en",
   source: "lnt",
   tbs: "qdr:w",
   sa: "X",
   ved: "",
   biw: "12"
}

你可以做这样的事情$_GET.q$_GET['biw']得到你所需要的。 注意,这种方法取代了重复的查询参数与搜索字符串最后给定值,这可能不希望/意外

URLSearchParams()

现在,我们也有URLSearchParams() (大多数的)新的浏览器,它可以让你做这样的事情:

window.$_GET = new URLSearchParams(location.search);
var value1 = $_GET.get('param1');


Answer 5:

正如其他人所解释的,你可以从JS解析页面URL获得的变量。

您也可以使用AJAX在它提交值的页面。 这真的取决于你传递什么样的信息,然后返回给用户。 (这肯定是做不简单或更直接的方式,只是一种替代方法)



Answer 6:

我用这一个Get请求(如$ _GET在PHP):

  var urlParams;
  (window.onpopstate = function () {
    var match,
          pl     = /\+/g,  Regex for replacing addition symbol with a space
           search = /([^&=]+)=?([^&]*)/g,
          decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
           query  = window.location.search.substring(1);
       urlParams = {};
       while (match = search.exec(query))
        urlParams[decode(match[1])] = decode(match[2]);
    })();


Answer 7:

document.get = function (d1,d2,d3) {
var divider1 = (d1 === undefined ? "?" : d1);
var divider2 = (d2 === undefined ? "&" : d2);
var divider3 = (d3 === undefined ? "=" : d3);
var url = window.location.href; //the current url
var pget = url.split(divider1)[1]; //slit the url and assign only the part after the divider 1
var pppget = {}; //define the contenitor object
if (pget.search(divider2) > -1) { //control if there is variable other than the first (?var1=a&var2=b) the var2 in this example
    var ppget = pget.split(divider2); //split the divider2 
    for (i = 0;i==ppget.lenght; i++) { //start a for and stop it when i == at object length
        if (ppget[i].search(divider3) > -1) { //control if is an empty var 
            psget = ppget[i].split(divider3);//if is split in 2 part using divider 3
            pppget[psget[0]] = psget[1];//assign to the object the value of first element and for value the second value  ex {var1=a,...}  
        } else {//if is a empty var (?var1&...)
            pppget[ppget[i]] = "";//assign only the value of first element with value a blank string
        }
    }
} else {//if the url don't contain other variable 
    if (pget.search(divider3) > -1) { //control if is an empty var 
        var ppget = pget.split(divider3);//if is split in 2 part using divider 3
        pppget[ppget[0]] = ppget[1];//assign to the object the value of first element and for value the second value  ex {var1=a}  
    } else {//if is a empty var (?var1)
        pppget[pget] = "";//assign only the value of first element with value a blank string
    }
}
return pppget;
/* return the object 
 * the use of the function is like this $_GET=document.get()
 * echo $_GET[var]
 * or use custom divider the default is setted for php standard divider
 */};


文章来源: Access GET directly from JavaScript?