3

I`m new to JavaScript and I need some help extracting the ID from URL using JavaScript for a gallery.

This is the link: www.shinylook.ro/produs/44/mocasini-barbati.html.

I need that number 44 in a variable.

pimvdb
  • 151,816
  • 78
  • 307
  • 352
user1483138
  • 57
  • 1
  • 1
  • 7
  • 1
    Welcome to Stack Overflow! [What have you tried?](http://whathaveyoutried.com) – Matt Ball Jun 26 '12 at 14:59
  • 1
    possible duplicate of [Get ID from URL with jQuery](http://stackoverflow.com/questions/3730359/get-id-from-url-with-jquery) – Esailija Jun 26 '12 at 14:59
  • 1
    @Esailija: Except this isn't flagged with `jquery`, and the number isn't at the end of the URL. Many of the answers there rely on one or the other of those. – T.J. Crowder Jun 26 '12 at 15:04
  • @T.J.Crowder The second answer splits by `/` for example :p. You must be joking about the jQuery part.. – Esailija Jun 26 '12 at 15:06
  • 1
    @Esailija: No, I'm not: [This answer](http://stackoverflow.com/a/3730384/157247) is precisely useless if the OP isn't using jQuery, just as [the accepted answer](http://stackoverflow.com/a/3730378/157247) is useless for this questioner (as it relies on the number being the last thing in the URL). – T.J. Crowder Jun 26 '12 at 15:13
  • @T.J.Crowder yes obviously if you ask for 1+1 in jQuery, you get some jokers linkin jQuery arithmetic plugins (that plugin doesn't use jQuery for anything at all other than namespace). And the accepted answer here is now exact duplicate of the second answer (*use split `"/"`*) in that question, so saying possible duplicate is not a crime here. – Esailija Jun 26 '12 at 16:09
  • 1
    @Esailija: We just disagree. :-) (I didn't call it a "crime" -- that would have been *way* overstating it if I had.) – T.J. Crowder Jun 26 '12 at 16:21

2 Answers2

12

You have to use the location object to get the URL, after that, you can use split to split the URL on the slashes.

location.pathname.split('/')[2] // Returns 44 in your example
Calvein
  • 2,111
  • 13
  • 28
  • This is what i had for this kind of link http://www.shinylook.ro/product.php?id=44 function transferaId() { var v = {}; var foo = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, cheie, val) { v[cheie] = val; }); return v; } – user1483138 Jun 26 '12 at 15:06
  • Foe this example, you can use this `location.search.replace('?id=', '')[1]` – Calvein Jun 26 '12 at 15:09
5

You can do that with String#split or with a regular expression.

String#split lets you split a string on a delimiter and get an array as a result. So in your case, you could split on / and get an array where 44 would be at index 2.

Regular expressions let you do much more complicated matching and extraction, as shown by the various demos on the linked page. For instance,

var str = "www.shinylook.ro/produs/44/mocasini-barbati.html";
var m = /produs\/(\d+)\//.exec(str);
if (m) {
    // m[1] has the number (as a string)
}

In both cases, the number will be a string. You can parse it with parseInt, e.g. n = parseInt(s, 10) (assuming it's base 10).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Could you please elaborate on what the `m[1]` guard is for? I think it is redundant with the `+` quantifier. – pimvdb Jun 26 '12 at 15:03
  • @pimvdb: It was indeed, thank you. Doh! I do tend to be a belt-and-braces guy, but that was OTT. :-) – T.J. Crowder Jun 26 '12 at 15:04