Javascript 将 URL 分解为其组件

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

Break a URL into its components

javascripturlurl-parsing

提问by Chris Dutrow

I'm using javascript and would like to take a URL string that I have and break it down into its components such as the host, path, and query arguments.

我正在使用 javascript 并希望获取我拥有的 URL 字符串并将其分解为它的组件,例如主机、路径和查询参数。

I need to do this in order to get to one of the query arguments, which is itself a URL and is thus encoded in the original URL string.

我需要这样做才能获得查询参数之一,它本身就是一个 URL,因此被编码在原始 URL 字符串中。

I feel like there should be an easy way to do this in Javascript. Perhaps something that looks like this:

我觉得在 Javascript 中应该有一种简单的方法来做到这一点。也许看起来像这样:

var what_I_Want = url("http://www.domain.com?queryArg1=somequeryargument").getQueryArgumentValue("queryArg1");

回答by Tak

The parseUrifunction will do everything you need

parseUri功能会做你需要的一切

EditAlternatively you can get the DOM to do the hard workfor you and access properties on a newly created aobject for different parts of the URL.

编辑或者,您可以让 DOM为您完成繁重的工作,并a为 URL 的不同部分访问新创建的对象上的属性。

回答by DPrithweema

<script type="text/javascript" language="javascript">
newURL = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;
</script>

Hope this will help..

回答by denysonique

In javascript you can do this by using split() for the params and using the location object for the protocol and domain -- like Carl suggested

在 javascript 中,您可以通过对参数使用 split() 并为协议和域使用位置对象来做到这一点——就像卡尔建议的那样

Also you can use parseUri as Tak suggested

您也可以按照 Tak 的建议使用 parseUri

There is also a jQuery plugin which makes parsing easier if you are already using jQuery in your project: https://github.com/allmarkedup/jQuery-URL-Parser#readme

如果您已经在项目中使用 jQuery,还有一个 jQuery 插件可以让解析更容易:https: //github.com/allmarkedup/jQuery-URL-Parser#readme

Example:

例子:

$.url('http://allmarkedup.com?sky=blue&grass=green').param('sky'); // returns 'blue'

回答by Carl

Probably not the greatest way of doing it but a simple method to get the query string in JavaScript would be to just use something along the lines of:

可能不是最好的方法,但在 JavaScript 中获取查询字符串的一个简单方法就是使用以下内容:

 a = "http://www.domain.com?queryArg1=somequeryargument";
 query = a.substring(a.indexOf('?')+1);

You could then split the query up based on the &'s and again on the = to get at whatever param you need.

然后,您可以根据 & 并再次根据 = 拆分查询,以获取您需要的任何参数。

Sorry if this ain't very helpful as its a bit of a low tech method :P

对不起,如果这不是很有帮助,因为它有点低技术:P

EDIT: Just wrote a quick little JavaScript object to get URL Query parameters for you (sort of like) in your example. Only tested it in chrome but in theory it should work :)

编辑:在您的示例中,刚刚编写了一个快速的小 JavaScript 对象来为您获取 URL 查询参数(有点像)。仅在 chrome 中对其进行了测试,但理论上它应该可以工作:)

//Quick and dirty query Getter object.
function urlQueryGetter(url){
    //array to store params
    var qParam = new Array();
    //function to get param
    this.getParam = function(x){
    return qParam[x];
    }

    //parse url 
    query = url.substring(url.indexOf('?')+1);
    query_items = query.split('&');
    for(i=0; i<query_items.length;i++){
        s = query_items[i].split('=');
        qParam[s[0]] = s[1];
    }

}

//Useage
var bla = new urlQueryGetter("http://www.domain.com?queryArg1=somequeryargument&test=cheese");
alert(bla.getParam('test'));