Javascript 如何在Javascript中获取url的hashtag值和&符号值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11727360/
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
How to get the hashtag value and ampersand value of a url in Javascript?
提问by Mike
I have a url like http://www.example.com/folder/file.html#val=90&type="test"&set="none"&value="reset?setvalue=1&setvalue=45"
我有一个像 http://www.example.com/folder/file.html#val=90&type="test"&set="none"&value="reset?setvalue=1&setvalue=45"
Now I need to get the portion of url from starting from #, How do I get that, I tried using window.location.search.substr();
but looks like that searches for ? in a url. is there a method to get the value of url after #
现在我需要从 # 开始获取 url 的部分,我如何得到它,我尝试使用window.location.search.substr();
但看起来像搜索 ? 在一个网址中。有没有办法在#之后获取url的值
How do I also get a portion of url from ampersand &
我如何还从 & 中获取一部分 url &
Thanks, Michael
谢谢,迈克尔
回答by Matthew Blancarte
var hash = window.location.hash;
More info here: https://developer.mozilla.org/en/DOM/window.location
更多信息在这里:https: //developer.mozilla.org/en/DOM/window.location
Update: This will grab all characters after the hashtag, including any query strings. From the MOZ manual:
更新:这将抓取主题标签后的所有字符,包括任何查询字符串。从 MOZ 手册:
window.location.hash === the part of the URL that follows the # symbol, including the # symbol.
You can listen for the hashchange event to get notified of changes to the hash in
supporting browsers.
Now, if you need to PARSE the query string, which I believe you do, check this out here: How can I get query string values in JavaScript?
现在,如果您需要解析查询字符串,我相信您会这样做,请在此处查看:如何在 JavaScript 中获取查询字符串值?
回答by Utkanos
To grab the hash:
获取哈希:
location.hash.substr(1); //substr removes the leading #
To grab the query string
获取查询字符串
location.search.substr(1); //substr removes the leading ?
[EDIT - since you seem to have a sort query-string-esq string which is actually part of your hash, the following will retrieve and parse it into an object of name/value pairings.
[编辑 - 由于您似乎有一个排序查询字符串 esq 字符串,它实际上是您的哈希的一部分,以下内容将检索它并将其解析为名称/值对的对象。
var params_tmp = location.hash.substr(1).split('&'),
params = {};
params_tmp.forEach(function(val) {
var splitter = val.split('=');
params[splitter[0]] = splitter[1];
});
console.log(params.set); //"none"
回答by Alex W
This will get the #
and &
values:
这将获得#
和&
值:
var page_url = window.location + ""; // Get window location and convert to string by adding ""
var hash_value = page_url.match("#(.*)"); // Regular expression to match anything in the URL that follows #
var amps; // Create variable amps to hold ampersand array
if(hash_value) // Check whether the search succeeded in finding something after the #
{
amps = (hash_value[1]).split("&"); // Split string into array using "&" as delimiter
alert(amps); // Alert array which will contain value after # at index 0, and values after each & as subsequent indices
}