6

If two url are like:

http://localhost:1113/Project/TestCourse.aspx?id=

http://localhost:1112/Project/TestCourse.aspx

How to check whether 'id' is present or not using javascript?

studgeek
  • 14,272
  • 6
  • 84
  • 96
ANP
  • 15,287
  • 22
  • 58
  • 79
  • possible duplicate of [How to get the value from the URL parameter?](http://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-url-parameter) – davidcondrey Jul 24 '15 at 06:42

3 Answers3

10

How about:

/\?id\=|&id\=/i.test(location.href)
KooiInc
  • 119,216
  • 31
  • 141
  • 177
8

I would tweak @KooiInc's answer to location.href.match(/(\?|&)id($|&|=)/). It catches parameters without values (for example http://localhost:1112/Project/TestCourse.aspx?id) and also ensures you only have id not just a param starting with id (e.g idparam).

studgeek
  • 14,272
  • 6
  • 84
  • 96
1

use location.href.match(/\?id\=/i) && location.href.match(/&id\=/i)

Bang Dao
  • 5,091
  • 1
  • 24
  • 33