JavaScript - 如何从 url 解析哈希参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16046252/
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-27 03:02:28  来源:igfitidea点击:

JavaScript - how to parse hash parameters from url?

javascript

提问by Matthew Woodard

How do you get a part of a URL hash using js?

如何使用 js 获取 URL 哈希的一部分?

Here is what I have:

这是我所拥有的:

something.html?il=#/new/product/123

something.html?il=#/new/product/123

And I need to get "123" as a variable ...

我需要将“123”作为变量......

***no libraries, pure javascript please.

*** 没有库,请使用纯 javascript。

Any ideas?

有任何想法吗?

回答by Robert Bolton

Here is some Javascript that should help you. Just take the return value from the getQuerystringNameValue() function and use $("#textboxID").val(returnValue); to assign it to the textbox.

这里有一些 Javascript 应该可以帮助你。只需从 getQuerystringNameValue() 函数中获取返回值并使用 $("#textboxID").val(returnValue); 将其分配给文本框。

alert("name1" + " = " + getQuerystringNameValue("name1"));
alert("name2" + " = " + getQuerystringNameValue("name2"));
alert("name3" + " = " + getQuerystringNameValue("name3"));


function getQuerystringNameValue(name)
{
    // For example... passing a name parameter of "name1" will return a value of "100", etc.
    // page.htm?name1=100&name2=101&name3=102

    var winURL = window.location.href;
    var queryStringArray = winURL.split("?");
    var queryStringParamArray = queryStringArray[1].split("&");
    var nameValue = null;

    for ( var i=0; i<queryStringParamArray.length; i++ )
    {           
        queryStringNameValueArray = queryStringParamArray[i].split("=");

        if ( name == queryStringNameValueArray[0] )
        {
            nameValue = queryStringNameValueArray[1];
        }                       
    }

    return nameValue;
}

回答by Adrian

In JavaScript, document.location.searchcontains the query parameters. You can use Regular Expressionsto parse out the pieces you need. However, in the URL in your example, it isn't the query string, it's the anchor, found in document.location.hash.

在 JavaScript 中,document.location.search包含查询参数。您可以使用正则表达式来解析您需要的部分。但是,在您的示例中的 URL 中,它不是查询字符串,而是锚点,在document.location.hash.

回答by Pablo Mescher

you need the hash, not the querystring. Anything after the # is called the hash of the url.

您需要散列,而不是查询字符串。# 之后的任何内容都称为 url 的哈希值。

You can use document.location.hash and parse that string

您可以使用 document.location.hash 并解析该字符串

var id = document.location.hash.split("/"); //convert the hash into an array, splitting with "/"
id = id.pop(); //pop will get the last item on the array

However readable and easy this method is, its not safe to assume the string you are searching will always be the last on the array. You could use a regular expression to further narrow the chances

无论此方法多么易读和容易,假设您搜索的字符串始终是数组中的最后一个是不安全的。您可以使用正则表达式来进一步缩小机会

var hash = document.location.hash,
    re = /\/new\/product\/(d+)/,
    id = hash.match(re);// for some reason this matches both the full string and the number
id = id.pop(); // so I must do this again