如果两个URL如下:
如何使用JavaScript检查是否存在'id'?
这样怎么样:
/\?id\=|&id\=/i.test(location.href)
我会调整@KooiInc的答案为location.href.match(/(\?|&)id($|&|=)/)
。它可以捕获没有值的参数(例如http://localhost:1112/Project/TestCourse.aspx?id),并确保您只有id,而不是以id开头的参数(例如idparam)。
使用 location.href.match(/\?id\=/i) && location.href.match(/&id\=/i)