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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:31:27  来源:igfitidea点击:

Using location.search to locate a parameter's value

javascriptlocation

提问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.searchwill 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

  1. location.searchwill be equal to ?name=jon&country=us
  2. .slice(1)skips the ?, returning the rest of the string.
  3. .split("&")[0]splits it into two strings (?name=jonand country=us) and takes first one
  4. .split("=")[1]splits name=joninto nameand jonand takes the second one. Done!
  1. location.search将等于 ?name=jon&country=us
  2. .slice(1)跳过?,返回字符串的其余部分。
  3. .split("&")[0]将其拆分为两个字符串 ( ?name=jonand country=us) 并取第一个
  4. .split("=")[1]分裂name=jonnameandjon并取第二个。完毕!

回答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.locationor window.location.searchdirectly

您也可以考虑给用户window.locationwindow.location.search直接

let searchParams = new URLSearchParams(window.location.search);
console.log(searchParams.get('yourname'));

回答by docodemore

To make ?yourname=gilgiladusing 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: enter image description here

确保使用控制台,然后[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