admin管理员组

文章数量:1297123

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

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

Share Improve this question asked Aug 7, 2012 at 13:18 Dave HomerDave Homer 1681 silver badge11 bronze badges 1
  • 1 This is JavaScript - you dont require a library to read the url .... – Manse Commented Aug 7, 2012 at 13:23
Add a ment  | 

4 Answers 4

Reset to default 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];
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) would also work

本文标签: javascriptHow to get the last two characters of url with jQueryStack Overflow