通过 URL 传递 JavaScript 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14489087/
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
Pass JavaScript variable through URL
提问by Scott
I have a page that sets a few variables using javascript Math.random()
as shown.
Math.random()
如图所示,我有一个使用 javascript 设置一些变量的页面。
var RandomValue1 = Math.floor(Math.random() * 5);
var RandomValue2 = Math.floor(Math.random() * 6);
var string = array1[RandomValue1];
var string = array2[RandomValue2];
I would like to allow a user to pass the RandomValue
value through the URL
to another person. When the second person goes to the shared URL
, RandomValue
is the value as the first person - so that the second person sees the same 'string' as the first person.
我想允许用户通过 将RandomValue
值传递URL
给另一个人。当第二个人转到 shared 时URL
, ,RandomValue
是作为第一个人的值 - 这样第二个人就会看到与第一个人相同的“字符串”。
I am fairly new to web design and web programming so I don't know if this needs to be handled on the server-side or client-side.
我对网页设计和网页编程相当陌生,所以我不知道这是否需要在服务器端或客户端处理。
I assume I need to pass a url something like page1.html?RandomValue1=1&RandomValue2=3
but I cannot get the page to then set those variables and load. Can this be done in JavaScript
?
我假设我需要传递一个类似的 urlpage1.html?RandomValue1=1&RandomValue2=3
但我无法获取页面然后设置这些变量并加载。这可以在JavaScript
吗?
采纳答案by dmk
You can read GET variables with javascript. Here is an example function (source):
您可以使用 javascript 读取 GET 变量。这是一个示例函数(源):
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
Usage:
用法:
var first = getUrlVars()["id"];
var second = getUrlVars()["page"];
alert(first);
alert(second);
回答by Joshua
Your assumption is correct. You need to put those variables into the URL as PARAMETERS. I would further suggest using a "#" instead of actual parameters so you don't risk updating the page each time, so the URL would look like mydomain.com/page.html#RandomValue1=1&RandomValue2=3.
你的假设是正确的。您需要将这些变量作为参数放入 URL。我进一步建议使用“#”而不是实际参数,这样您就不会冒险每次更新页面,因此 URL 看起来像 mydomain.com/page.html#RandomValue1=1&RandomValue2=3。
You can use Javascript to get / set those parameters by the window.location.href method. On page load, the Javascript will check to see whether there is a "#" and parse anything after it in the URL. After the page has loaded, a Javascript can be used to update those parameters.
您可以使用 Javascript 通过 window.location.href 方法获取/设置这些参数。在页面加载时,Javascript 将检查是否有“#”并解析 URL 中后面的任何内容。页面加载后,可以使用 Javascript 来更新这些参数。
See the following link for full documentation on how to use window.location:
有关如何使用 window.location 的完整文档,请参阅以下链接:
https://developer.mozilla.org/en-US/docs/DOM/window.location
https://developer.mozilla.org/en-US/docs/DOM/window.location
Oh, and here's a javascript library that might help get you there faster. It works with the hash ('#') and will automatically parse (deparameterize) the parameters for you, assigning them to strings / objects etc. which you can then use.
哦,这里有一个 javascript 库,可以帮助您更快地到达那里。它与散列('#')一起工作,并会自动为您解析(去参数化)参数,将它们分配给字符串/对象等,然后您可以使用它们。
回答by FunkyMonk91
I would do it in PHP.
我会用 PHP 来做。
You can put a hidden field on your page (in a form). Set the form method to GET and have the action set to another PHP page.
您可以在页面上(在表单中)放置一个隐藏字段。将表单方法设置为 GET 并将操作设置为另一个 PHP 页面。
<form id="form" name="form" action="my_page.php" method="GET">
<input id="myId" name="myName" type="hidden />
</form>
In the PHP page you can manage the variables with the $_GET object.
在 PHP 页面中,您可以使用 $_GET 对象管理变量。
<?php
if(isset($_GET['myName'])){
//do something with value;
}
?>
Just remember that the GET and POST variables are based off the name attribute from the HTML elements. In my example, the hidden field has the name myName
, so when I want to refer to it I use $_GET['myName'];
请记住,GET 和 POST 变量基于 HTML 元素的 name 属性。在我的示例中,隐藏字段的名称为myName
,因此当我想引用它时,我使用$_GET['myName'];
回答by Justin Ethier
You will need to use the server to coordinate the data between both clients. In other words, the first user would need to submit their random value to the server - this could be done using a GET URL like page1.html?RandomValue1=1
although you might want to research POST requests.
您将需要使用服务器来协调两个客户端之间的数据。换句话说,第一个用户需要向服务器提交他们的随机值 - 这可以使用 GET URL 来完成,page1.html?RandomValue1=1
尽管您可能想要研究 POST 请求。
Anyway, the second user could then go to the shared URL to see the value stored on the server.
无论如何,第二个用户然后可以转到共享 URL 以查看存储在服务器上的值。