1

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

Dave Homer
  • 168
  • 1
  • 10

4 Answers4

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];
Adil
  • 143,427
  • 25
  • 201
  • 198
4
res = document.location.href.split('?')[1];
sloth
  • 95,484
  • 19
  • 164
  • 210
Erdinç Özdemir
  • 1,313
  • 4
  • 26
  • 51
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 '?'
Community
  • 1
  • 1
Maël Nison
  • 6,785
  • 6
  • 41
  • 72
-1

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

cori
  • 8,418
  • 7
  • 44
  • 79