-2

I have a URL like this:

http://localhost/src/profiles/project_part_2/Project2.html?score=6

I want to have the amount of score variable.

I did this:

var url_path = window.location;

That return the path:

http://localhost/src/profiles/project_part_2/Project2.html?score=6

Now I want to extract the amount of score.

Michał Perłakowski
  • 70,955
  • 24
  • 137
  • 155
alone
  • 159
  • 7
  • 1
    You can parse query string first.then get your variable value. http://stackoverflow.com/questions/2090551/parse-query-string-in-javascript – Morteza Tourani Jun 25 '16 at 21:13

2 Answers2

2

Try this:

location.href.match(/score=([^&]+)/)[1]
Michał Perłakowski
  • 70,955
  • 24
  • 137
  • 155
2

This is a function that I use for query strings

function urlParam(name){
  var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
  if (results==null){
     return null;
  }
  else{
     return results[1] || 0;
  }
}

So you would do

var score = urlParam('score');
alecschrader
  • 371
  • 3
  • 7