有没有办法在 url 中传递 javascript 变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17863986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to pass javascript variables in url?
提问by vsapountzis
Is there a way to make the below script pass the javascript values to the url of the href link?
有没有办法让下面的脚本将 javascript 值传递给 href 链接的 url?
<script type="text/javascript">
function geoPreview(lat,long) {
var elemA = document.getElementById("lat").value;
var elemB = document.getElementById("long").value;
window.location.href = "http://www.gorissen.info/Pierre/maps/googleMapLocation.php?lat=elemA&lon=elemB&setLatLon=Set";
}
</script>
回答by pattyd
Try this:
试试这个:
window.location.href = "http://www.gorissen.info/Pierre/maps/googleMapLocation.php?lat="+elemA+"&lon="+elemB+"&setLatLon=Set";
To put a variable in a string enclose the variable in quotes and addition signs like this:
要将变量放入字符串中,请将变量用引号和加号括起来,如下所示:
var myname = "BOB";
var mystring = "Hi there "+myname+"!";
Just remember that one rule!
只要记住这一规则!
回答by Drew
Do you mean include javascript variable values in the query string of the URL?
您的意思是在 URL 的查询字符串中包含 javascript 变量值吗?
Yes:
是的:
window.location.href = "http://www.gorissen.info/Pierre/maps/googleMapLocation.php?lat="+var1+"&lon="+var2+"&setLatLon="+varEtc;
回答by FlashOver
Summary
概括
With either string concatenationor string interpolation(via template literals).
使用字符串连接或字符串插值(通过模板文字)。
Here with JavaScript template literal:
这里使用 JavaScript 模板文字:
function geoPreview() {
var lat = document.getElementById("lat").value;
var long = document.getElementById("long").value;
window.location.href = `http://www.gorissen.info/Pierre/maps/googleMapLocation.php?lat=${lat}&lon=${long}&setLatLon=Set`;
}
Both parameters are unused and can be removed.
这两个参数都未使用,可以删除。
Remarks
评论
String Concatenation
字符串连接
Join strings with the +
operator:
使用+
运算符连接字符串:
window.location.href = "http://www.gorissen.info/Pierre/maps/googleMapLocation.php?lat=" + elemA + "&lon=" + elemB + "&setLatLon=Set";
String Interpolation
字符串插值
For more concise code, use JavaScript template literalsto replace expressions with their string representations.
Template literals are enclosed by ``
and placeholders surrounded with ${}
:
要获得更简洁的代码,请使用 JavaScript模板文字将表达式替换为其字符串表示形式。模板文字由``
和占位符包围${}
:
window.location.href = `http://www.gorissen.info/Pierre/maps/googleMapLocation.php?lat=${elemA}&lon=${elemB}&setLatLon=Set`;
Template literals are available since ECMAScript 2015 (ES6).
自 ECMAScript 2015 (ES6) 起就可以使用模板文字。
回答by Souvik Mukherjee
Try this:
试试这个:
window.location.href = "http://www.gorissen.info/Pierre/maps/googleMapLocation.php?lat=\''+elemA+'\'&lon=\''+elemB+'\'&setLatLon=Set";
回答by Hyman Crane
This is rather over-complicated but will make sense to the inexperienced programmer. In a url, you can include a ‘#' sign, and anything else can go after that while just going to that page. So you can use js:
这相当复杂,但对没有经验的程序员来说是有意义的。在 url 中,您可以包含一个“#”符号,然后在转到该页面时可以进行任何其他操作。所以你可以使用js:
var pass = prompt("what do you want to pass")
var site = "https://google.com"
var readysite = site + pass
document.location.replace(readysite)
Then in the other page use JavaScript to read the url and take what information is necessary.
然后在另一个页面中使用 JavaScript 读取 url 并获取必要的信息。
NOTE: the js above is not tested.
注意:上面的 js 没有经过测试。