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
  • 11

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
  • 146,340
  • 25
  • 209
  • 204
4
res = document.location.href.split('?')[1];
sloth
  • 99,095
  • 21
  • 171
  • 219
Erdinç Özdemir
  • 1,363
  • 4
  • 24
  • 52
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
  • 7,055
  • 7
  • 46
  • 77
-1

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

cori
  • 8,666
  • 7
  • 45
  • 81