如果两个网址如下:
http://localhost:1113/Project/TestCourse.aspx?id=
http://localhost:1112/Project/TestCourse.aspx
如何使用javascript检查'id'是否存在?
答案 0 :(得分:10)
怎么样:
/\?id\=|&id\=/i.test(location.href)
答案 1 :(得分:7)
我会调整@KooiInc对location.href.match(/(\?|&)id($|&|=)/)
的回答。它捕获没有值的参数(例如http://localhost:1112/Project/TestCourse.aspx?id),并且还确保您只有id而不仅仅是以id开头的param(例如idparam)。
答案 2 :(得分:1)
使用location.href.match(/\?id\=/i) && location.href.match(/&id\=/i)