我想我可以使用 PHP$_GET
从 JavaScript访问变量:
<script>
var to = $_GET['to'];
var from = $_GET['from'];
</script>
<script src="realScript" type="text/javascript"></script>
但也许它更简单。有没有办法直接从JS做到这一点?
我想我可以使用 PHP$_GET
从 JavaScript访问变量:
<script>
var to = $_GET['to'];
var from = $_GET['from'];
</script>
<script src="realScript" type="text/javascript"></script>
但也许它更简单。有没有办法直接从JS做到这一点?
看着
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
这是另一个想法:
<script type="text/javascript">
var $_GET = <?php echo json_encode($_GET); ?>;
alert($_GET['some_key']);
// or
alert($_GET.some_key);
</script>
我知道这个话题很老,但我想分享我自己的针对 JavaScript 中的 $_GET 的 ES6 优化解决方案。似乎所有关于这个主题的更受欢迎的问题都被 SO 新手的贡献锁定了,所以这里是:
window.$_GET = location.search.substr(1).split("&").reduce((o,i)=>(u=decodeURIComponent,[k,v]=i.split("="),o[u(k)]=v&&u(v),o),{});
我很想把你们都链接到关于array.reduce()、箭头函数、逗号运算符、解构赋值和短电路评估的 MDN 文档,但是,唉,另一个 SO 新手限制。
对于像google.com/webhp?q=foo&hl=en&source=lnt&tbs=qdr%3Aw&sa=X&ved=&biw=12
您有对象的 URL :
$_GET = {
q: "foo",
hl: "en",
source: "lnt",
tbs: "qdr:w",
sa: "X",
ved: "",
biw: "12"
}
你可以做一些事情,$_GET.q
或者$_GET['biw']
得到你需要的东西。请注意,此方法使用搜索字符串中最后给定的值替换重复的查询参数,这可能是不希望的/意外的
现在我们在(大多数)较新的浏览器中也有URLSearchParams(),它可以让你做这样的事情:
window.$_GET = new URLSearchParams(location.search);
var value1 = $_GET.get('param1');
我猜你是这么想的:
<script type="text/javascript">
var to = "<?= $_GET['to']; ?>";
var from = "<?= $_GET['from']; ?>";
</script>
...这只是您想法的语法更正:)
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
*/};