javascript 使用 location.search 定位参数的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26803110/
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
Using location.search to locate a parameter's value
提问by Giladiald
I'm working on a tool which takes the value parameters in the URL and does a few things with them.
我正在开发一个工具,它接受 URL 中的值参数并用它们做一些事情。
My issue is, I can't seem to use document.location to show the specific value that I'm after, for example:
我的问题是,我似乎无法使用 document.location 来显示我所追求的特定值,例如:
www.examplesite.com?yourname=gilgilad
www.examplesite.com?yourname=gilgilad
I want to use document.location.search and put it in a var, I need that var's value to be "gilgilad".
我想使用 document.location.search 并将其放入一个 var 中,我需要该 var 的值是“gilgilad”。
Is this even possible using location.search?
这甚至可以使用 location.search 吗?
回答by nicael
location.search
will return all after question mark including it. So there is universal js to get value of the first parameter (even if url has more parameters):
location.search
将在包括它在内的问号之后返回所有内容。所以有通用的js来获取第一个参数的值(即使url有更多的参数):
var desire = location.search.slice(1).split("&")[0].split("=")[1]
Example: let's take url http://example.com?name=jon&country=us
示例:让我们获取 urlhttp://example.com?name=jon&country=us
location.search
will be equal to?name=jon&country=us
.slice(1)
skips the?
, returning the rest of the string..split("&")[0]
splits it into two strings (?name=jon
andcountry=us
) and takes first one.split("=")[1]
splitsname=jon
intoname
andjon
and takes the second one. Done!
location.search
将等于?name=jon&country=us
.slice(1)
跳过?
,返回字符串的其余部分。.split("&")[0]
将其拆分为两个字符串 (?name=jon
andcountry=us
) 并取第一个.split("=")[1]
分裂name=jon
成name
andjon
并取第二个。完毕!
回答by zumalifeguard
A more generic solution to split the location.search query parameters and convert them into an object:
拆分 location.search 查询参数并将它们转换为对象的更通用的解决方案:
var a = location.search.split("&");
var o = a.reduce(function(o, v) {
var kv = v.split("=");
kv[0] = kv[0].replace("?", "");
o[kv[0]] = kv[1];
return o;
},
{});
回答by Mauro
let url = new URL('www.examplesite.com?yourname=gilgilad');
let searchParams = new URLSearchParams(url.search);
console.log(searchParams.get('yourname'));
you can consider also to user window.location
or window.location.search
directly
您也可以考虑给用户window.location
或window.location.search
直接
let searchParams = new URLSearchParams(window.location.search);
console.log(searchParams.get('yourname'));
回答by docodemore
To make ?yourname=gilgilad
using document.location.search:
为了让?yourname=gilgilad
使用document.location.search:
window.location.search = 'yourname=gilgilad';
here is jsfiddle: http://jsfiddle.net/t81k3bgc/
这是 jsfiddle:http: //jsfiddle.net/t81k3bgc/
make sure to use console and then [run]
. you will see:
确保使用控制台,然后[run]
. 你会看见:
For more information: https://developer.mozilla.org/en-US/docs/Web/API/Window.location#Example_.235.3A_Send_a_string_of_data_to_the_server_by_modifying_the_search_property.3A
更多信息:https: //developer.mozilla.org/en-US/docs/Web/API/Window.location#Example_.235.3A_Send_a_string_of_data_to_the_server_by_modifying_the_search_property.3A