Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.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 如何在html链接中发送参数以及如何检索参数?_Javascript_Html_Href - Fatal编程技术网

Javascript 如何在html链接中发送参数以及如何检索参数?

Javascript 如何在html链接中发送参数以及如何检索参数?,javascript,html,href,Javascript,Html,Href,这是我的带有href的html标签 <a href='components/home/singleActivity.html?view="search"'> .... </a> 我想发送一个字符串参数,是否正确 如何在javascript中检索该参数?以及如何为同一href发送两个参数?试试看 试试看 为了发送URL中的参数,您不需要将值括在引号(“”)或双引号(“”)中。您只需发送如下所示的值(或多个值) components/home/singleActiv

这是我的带有href的html标签

<a href='components/home/singleActivity.html?view="search"'>
....
</a>

我想发送一个字符串参数,是否正确

如何在javascript中检索该参数?以及如何为同一href发送两个参数?

试试看

试试看


为了发送URL中的参数,您不需要将值括在引号
(“”)
或双引号
(“”)
中。您只需发送如下所示的值(或多个值)

components/home/singleActivity.html?view=search&secondvalue=anythingthatyouthinkof
还要记住,您需要跟踪URL编码


对于检索参数,详细解释了这一点

为了发送URL中的参数,您不需要将值括在quote
(“”)
或doublequotes
(“”)
中。您只需发送如下所示的值(或多个值)

components/home/singleActivity.html?view=search&secondvalue=anythingthatyouthinkof
还要记住,您需要跟踪URL编码


对于检索参数,详细解释了这一点

应该没有引号
components/home/singleActivity.html?view=search
来发送两个参数,并用
&

components/home/singleActivity.html?foo=bar&baz=quux
要在javascript中读取它们,请使用以下代码:

var params = {};
location.search.slice(1).split("&").forEach(function(pair) {
   pair = pair.split("=");
   params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
});

它应该没有引号
components/home/singleActivity.html?view=search
来发送两个参数,并用
将它们连接起来

components/home/singleActivity.html?foo=bar&baz=quux
要在javascript中读取它们,请使用以下代码:

var params = {};
location.search.slice(1).split("&").forEach(function(pair) {
   pair = pair.split("=");
   params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
});

要发送多个值,可以使用
?view=search&key2=value2&key3=value3

此外,要访问这些参数,可以使用window.location访问URL

差不多

var params = window.location.split('?')[1];

这会给你以后的一切吗?在URL中。

要发送多个值,可以使用
?view=search&key2=value2&key3=value3

此外,要访问这些参数,可以使用window.location访问URL

差不多

var params = window.location.split('?')[1];

这会给你以后的一切吗?在URL中。

拆分是字符串的方法,不是函数。我的错。编辑;-)Thanksplit是字符串的方法,不是函数。我的错。编辑;-)ThanksOn新浏览器您还可以使用URL对象,这使思考变得容易了10倍。@BrunoSouza我不认为您可以使用URL对象获取所有键。如果您了解foo和baz,您只能获取值栏和quux。在新浏览器上,您也可以使用URL对象,这使思考变得容易了10倍。@BrunoSouza我不认为您可以使用URL对象获取所有键。如果您了解foo和baz,您只能获取值栏和quux。看见