简体   繁体   中英

How to get the last two characters of url with jQuery?

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

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];
res = document.location.href.split('?')[1];

Duplicate of 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 '?'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM