Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.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中获取url的标签值和符号值?_Javascript_Jquery_Url_Query String - Fatal编程技术网

如何在Javascript中获取url的标签值和符号值?

如何在Javascript中获取url的标签值和符号值?,javascript,jquery,url,query-string,Javascript,Jquery,Url,Query String,我有一个类似http://www.example.com/folder/file.html#val=90&type=“测试”&set=“无”&value=“重置?setvalue=1&setvalue=45” 现在我需要从#开始获取url的部分,如何获取它,我尝试使用window.location.search.substr()但看起来像是在搜索?在url中。是否有一种方法可以在之后获取url的值# 如何从ampersand& 谢谢, 迈克尔 更多信息请点击此处: 更新:这将获取hashtag后

我有一个类似
http://www.example.com/folder/file.html#val=90&type=“测试”&set=“无”&value=“重置?setvalue=1&setvalue=45”

现在我需要从#开始获取url的部分,如何获取它,我尝试使用
window.location.search.substr()但看起来像是在搜索?在url中。是否有一种方法可以在之后获取url的值#

如何从ampersand&

谢谢, 迈克尔

更多信息请点击此处:

更新:这将获取hashtag后面的所有字符,包括任何查询字符串。从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.
现在,如果您需要解析查询字符串(我相信您是这样做的),请检查此处:

这将获得
&
值:

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
}
要获取哈希,请执行以下操作:

location.hash.substr(1); //substr removes the leading #
获取查询字符串的步骤

location.search.substr(1); //substr removes the leading ?
[编辑-由于您似乎有一个排序查询字符串esq string,它实际上是哈希的一部分,下面将检索并将其解析为名称/值配对的对象

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"

是否有一种方法可以获取后面的值&这是一个单独的问题-请将其作为单独的问题发布,或编辑当前问题。
后面的部分(不是
&
是查询字符串-您询问了哈希)@Mike it会抓取哈希标记后面的所有内容,包括哈希标记和不带“?”的查询字符串:)假设我只有www.domain.com&val=1&val=4,我需要获取&val=1&val=4我需要什么才能抓取查询字符串。无法读取null的属性“1”是我在控制台中运行该属性时得到的错误这将在没有h时出错因为匹配是假定的,而不是检查的。而且,这不会再出错,因为我添加了
if
语句。@AlexW我认为这种方法可以从url中查找任何内容,而不仅仅是标签,right@Mike正确。您也可以使用此方法找到符号AND。这是因为您将查询字符串与哈希混淆。您似乎有一种查询字符串esq string作为哈希的一部分。如果要提取它,需要一些正则表达式、字符串处理。我会编辑答案。@MatthewBlancarte是否有任何快捷方式可以在符号后获取任何内容?如果没有,那么是什么方法?您在链接中尝试过接受的方法吗我在回答的底部给出了?这将解析查询字符串。如果您在服务器端(例如PHP/Rails等)执行此操作,会简单得多。
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"