在 JavaScript 中解析 URL

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

Parse an URL in JavaScript

javascriptjqueryparse-url

提问by laukok

How do I parse an URL with JavaScript (also with jQuery)?

如何使用 JavaScript(也使用 jQuery)解析 URL?

For instance I have this in my string,

例如,我的字符串中有这个,

url = "http://example.com/form_image_edit.php?img_id=33"

I want to get the value of img_id

我想获得的价值 img_id

I know I can do this easily with PHP with parse_url(), but I want to know how it is possible with JavaScript.

我知道我可以使用 PHP 轻松完成此操作parse_url(),但我想知道使用 JavaScript 是如何实现的。

回答by Sindre Sorhus

You can use a trick of creating an a-element, add the url to it, and then use its Location object.

您可以使用创建a-element的技巧,向其中添加 url,然后使用其Location 对象

function parseUrl( url ) {
    var a = document.createElement('a');
    a.href = url;
    return a;
}

parseUrl('http://example.com/form_image_edit.php?img_id=33').search

Which will output: ?img_id=33

这将输出: ?img_id=33



You could also use php.jsto get the parse_url function in JavaScript.

您还可以使用php.js来获取JavaScript 中parse_url 函数



Update (2012-07-05)

更新 (2012-07-05)

I would recommend using the excellent URI.jslibrary if you need to do anything more than super simple URL handling.

如果您需要做的不仅仅是超简单的 URL 处理,我建议您使用优秀的URI.js库。

回答by Ray Toal

If your string is called sthen

如果你的字符串被调用,s那么

var id = s.match(/img_id=([^&]+)/)[1]

will give it to you.

会给你的。

回答by mdaguerre

Try this:

尝试这个:

var url = window.location;
var urlAux = url.split('=');
var img_id = urlAux[1]

回答by A. Masson

Existing good jQuery plugin Purl (A JavaScript URL parser).This utility can be used in two ways - with jQuery or without...

现有的优秀 jQuery 插件Purl(一个 JavaScript URL 解析器)。这个实用程序可以通过两种方式使用 - 使用 jQuery 或不使用...

回答by Ilya Kharlamov

One liner:

一个班轮:

location.search.replace('?','').split('&').reduce(function(s,c){var t=c.split('=');s[t[0]]=t[1];return s;},{})

回答by kobe

got it from google, try to use this method

从谷歌得到它,尝试使用这种方法

function getQuerystring2(key, default_) 
{ 
    if (default_==null) 
    { 
        default_=""; 
    } 
    var search = unescape(location.search); 
    if (search == "") 
    { 
        return default_; 
    } 
    search = search.substr(1); 
    var params = search.split("&"); 
    for (var i = 0; i < params.length; i++) 
    { 
        var pairs = params[i].split("="); 
        if(pairs[0] == key) 
        { 
            return pairs[1]; 
        } 
    } 


return default_; 
}

回答by michielbdejong

This should fix a few edge-cases in kobe's answer:

这应该可以解决 kobe 回答中的一些边缘情况:

function getQueryParam(url, key) {
  var queryStartPos = url.indexOf('?');
  if (queryStartPos === -1) {
    return;
  }
  var params = url.substring(queryStartPos + 1).split('&');
  for (var i = 0; i < params.length; i++) {
    var pairs = params[i].split('=');
    if (decodeURIComponent(pairs.shift()) == key) {
      return decodeURIComponent(pairs.join('='));
    }
  }
}

getQueryParam('http://example.com/form_image_edit.php?img_id=33', 'img_id');
// outputs "33"

回答by Mittal Patel

Something like this should work for you. Even if there are multiple query string values then this function should return the value of your desired key.

像这样的事情应该适合你。即使有多个查询字符串值,此函数也应返回所需键的值。

function getQSValue(url) 
{
    key = 'img_id';
    query_string = url.split('?');
    string_values = query_string[1].split('&');
    for(i=0;  i < string_values.length; i++)
    {
        if( string_values[i].match(key))
            req_value = string_values[i].split('=');    
    }
    return req_value[1];
}

回答by Kevin Cox

I wrote a javascript url parsing library, URL.js, you can use it for this.

我写了一个 javascript url 解析库,URL.js,你可以使用它。

Example:

例子:

url.parse("http://mysite.com/form_image_edit.php?img_id=33").get.img_id === "33"

回答by Md. Afsar Uddin

You can use the jquery plugin http://plugins.jquery.com/url. $.url("?img_id")will return 33

您可以使用 jquery 插件http://plugins.jquery.com/url$.url("?img_id")将返回33