Javascript 使用 jQuery 从 URL 获取 ID

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

Get ID from URL with jQuery

javascriptjquery

提问by wesbos

I've got a url like this:

我有一个这样的网址:

http://www.site.com/234234234

I need to grab the Id after /, so in this case 234234234

我需要在 之后获取 Id /,所以在这种情况下234234234

How can i do this easily?

我怎样才能轻松做到这一点?

回答by BalusC

Get a substringafter the last index of/.

获取的最后一个索引之后的子字符串/

var url = 'http://www.site.com/234234234';
var id = url.substring(url.lastIndexOf('/') + 1);
alert(id); // 234234234

It's just basic JavaScript, no jQuery involved.

它只是基本的 JavaScript,不涉及 jQuery。

回答by Rohrbs

var url = window.location.pathname;
var id = url.substring(url.lastIndexOf('/') + 1);

回答by Fred Bergman

var full_url = document.URL; // Get current url
var url_array = full_url.split('/') // Split the string into an array with / as separator
var last_segment = url_array[url_array.length-1];  // Get the last part of the array (-1)
alert( last_segment ); // Alert last segment

回答by John Strickler

var url = "http://www.site.com/234234234"
var stuff = url.split('/');
var id = stuff[stuff.length-1];
//id = 234234234

回答by Boopathi Rajan

try this javascript

试试这个javascript

Snippet for getting the parameters from URL. Use javascript to get the URL parameters either from current window location or static URL into the argument for the function call.

用于从 URL 获取参数的代码段。使用 javascript 将 URL 参数从当前窗口位置或静态 URL 获取到函数调用的参数中。

javascript

javascript

function getUrlParameters(parameter, staticURL, decode){

       var currLocation = (staticURL.length)? staticURL : window.location.search,
           parArr = currLocation.split("?")[1].split("&"),
           returnBool = true;

       for(var i = 0; i < parArr.length; i++){
            parr = parArr[i].split("=");
            if(parr[0] == parameter){
                return (decode) ? decodeURIComponent(parr[1]) : parr[1];
                returnBool = true;
            }else{
                returnBool = false;            
            }
       }

       if(!returnBool) return false;  
    }

To get the parameter “id” from above static URL, use the following:

要从上面的静态 URL 获取参数“id”,请使用以下命令:

var idParameter = getUrlParameters("id", "http://www.example.com?id=1234&auth=true", true);

or

或者

var idParameter = getUrlParameters("id", "", true);

回答by sourcecode

My url is like this http://www.default-search.net/?sid=503. I want to get 503 . I wrote the following code .

我的网址是这样的http://www.default-search.net/?sid=503。我想得到 503 。我写了以下代码。

var baseUrl = (window.location).href; // You can also use document.URL
var koopId = baseUrl.substring(baseUrl.lastIndexOf('=') + 1);
alert(koopId)//503

If you use

如果你使用

var v = window.location.pathname;
console.log(v)

You will get only "/";

你只会得到“/”;

回答by RAVI singh

const url = "http://www.example.com/1234"
const id = url.split('/').pop();

Try this, it is much easier

试试这个,简单多了

The output gives 1234

输出为 1234

回答by Simon Gomes

You could just use window.location.hashto grab the id.

你可以window.location.hash用来获取id。

var id = window.location.hash;

I don't think you will need that much code to achieve this.

我认为您不需要那么多代码来实现这一点。

回答by Cristian Sanchez

Just because I can:

只因为我可以:

function pathName(url, a) {
   return (a = document.createElement('a'), a.href = url, a.pathname); //optionally, remove leading '/'
}

pathName("http://www.site.com/234234234") -> "/234234234"

回答by Jeff

Using the jQuery URL Parser plugin, you should be able to do this:

使用jQuery URL Parser 插件,您应该能够做到这一点:

jQuery.url.segment(1)