0

The url of my website is in one of the following two formats:

Format 1: http://www.example.com/books/1309/angular-book 

OR

Format 2: http://www.example.com/ShowBook.aspx?ID=1309

I can extract the entire URL using

var url = encodeURIComponent(window.location.href);

How can I extract the ID from the url (in this case "1309"), specifically when I do not know if the URL will appear as Format 1 or 2?

GG.
  • 21,083
  • 14
  • 84
  • 130
CuriousDev
  • 1,255
  • 1
  • 21
  • 44

5 Answers5

1

You can use this regexp to match only your urls format:

 /books\/(\d+)\/|ID=(\d+)/g

Using \d+ will match any number in your urls, here is a working example:

var url1 = 'http://www.example.com/books/1309/angular-book';
var url2 = 'http://www.example.com/ShowBook.aspx?ID=1309';

function getBookId(url) {
    var rexep = /books\/(\d+)\/|ID=(\d+)/g;
    var matches = rexep.exec(url);
    return matches[1] || matches[2];
}

console.log( getBookId(url1) );
console.log( getBookId(url2) );
YouneL
  • 8,152
  • 2
  • 28
  • 50
0

This is a duplicate question, you can check this one

You can use this method:

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}    

Usage:

getUrlVars()["ID"]
Dabbas
  • 3,112
  • 7
  • 42
  • 75
0

Use a regex:

url.match(/\d+/g) // One or more digits

With your examples:

"http://www.example.com/ShowBook.aspx?ID=1309".match(/\d+/g) // => ["1309"]

"http://www.example.com/books/1309/angular-book".match(/\d+/g) // => ["1309"]

Then you can parseInt the result.

GG.
  • 21,083
  • 14
  • 84
  • 130
  • Is there a way to pick the first digit that is encountered? Sometimes my cookie program may append a string of text after "ShowBook.aspx?ID=1309" which may contain a number too. – CuriousDev Jan 07 '18 at 17:13
  • @IWantToLearn yes you just pick the first element of the array. Example: `"http://www.example.com/ShowBook.aspx?ID=1309&cookie=2576".match(/\d+/g) // => ["1309", "2576"]` – GG. Jan 07 '18 at 17:32
  • Thanks but how do I pick the first element of the array? Let us say I do var arr = url.match(match(/\d+/g)); parseInt(arr); – CuriousDev Jan 07 '18 at 17:49
  • `var arr = url.match(/\d+/g); var id = parseInt(arr[0])` – GG. Jan 07 '18 at 18:51
  • Many thanks. Got it working. – CuriousDev Jan 07 '18 at 18:55
0

Try the following code;

var url = new URL("http://www.example.com/books/1309/angular-book");
//var url = new URL("http://www.example.com/ShowBook.aspx?ID=1309");
var id;
if(url.href.indexOf(".aspx") > -1)
{
   id = url.searchParams.get("ID");
}
else
{
   id = url.pathname.split("/")[2];
}
console.log(id);
lucky
  • 12,734
  • 4
  • 24
  • 46
-1
   function getID(){
        var url = window.location.href.split("/");
        if(url[3]==="books") return url[4];
        else { 
          url= window.location.href.split("?ID=");
          return url[1];
        }
    }
h-des
  • 767
  • 5
  • 9