如何用jQuery获取url的最后两个字符?

时间:2022-03-16 16:50:48

I have a url with the following format:

我有一个以下格式的网址:

base/list.html?12

and I want to create a variable which will be equal to the digits after the question mark in the url of the page. Something like:

我想创建一个变量,它将等于页面网址中问号后面的数字。就像是:

var xxx = anything after the ? ;

Then I need to load dynamic data into that page using this function:

然后我需要使用此函数将动态数据加载到该页面:

if(document.URL.indexOf(xxx) >= 0){ 
alert('Data loaded!');
}

How can I achieve this? and are the codes above correct?

我怎样才能做到这一点?以上代码是否正确?

Thanks

谢谢

4 个解决方案

#1


3  

res = document.location.href.split('?')[1];

#2


9  

You can use split to get the characters after ? in the url

您可以使用拆分来获取后面的字符吗?在网址中

var xxx = 'base/list.html?12';
var res = xxx.split('?')[1];

or for current page url

或当前页面网址

var res = document.location.href.split('?')[1];

#3


1  

Duplicate of 6644654.

重复6644654。

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

var search = parseUrl('base/list.html?12').search;
var searchText = search.substr( 1 ); // removes the leading '?'

#4


0  

document.location.search.substr(1) would also work

document.location.search.substr(1)也可以

#1


3  

res = document.location.href.split('?')[1];

#2


9  

You can use split to get the characters after ? in the url

您可以使用拆分来获取后面的字符吗?在网址中

var xxx = 'base/list.html?12';
var res = xxx.split('?')[1];

or for current page url

或当前页面网址

var res = document.location.href.split('?')[1];

#3


1  

Duplicate of 6644654.

重复6644654。

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

var search = parseUrl('base/list.html?12').search;
var searchText = search.substr( 1 ); // removes the leading '?'

#4


0  

document.location.search.substr(1) would also work

document.location.search.substr(1)也可以