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

时间:2016-04-26 07:47:23

标签: javascript html href

这是我的带href

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

我想发送一个字符串参数,这是正确的方法吗?

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

4 个答案:

答案 0 :(得分:2)

应该没有引号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]);
});

答案 1 :(得分:0)

尝试

<a href='components/home/singleActivity.html?view=Search&test2=New'>
MyLink
</a>

答案 2 :(得分:0)

要在网址中发送参数,您不要需要将值括在引用('')或双引号("")中。您只需要发送值(或多个值),如下所示。

components/home/singleActivity.html?view=search&secondvalue=anythingthatyouthinkof

另请注意,您需要跟踪urlencoding。

要检索参数 this thread ,请详细说明。

答案 3 :(得分:0)

要发送多个值,您可以使用getConnectedAgent()等等。

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

这样的东西
?view=search&key2=value2&key3=value3

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