regex - Why is this simple javascript not working? -


new js here. trying detect existence of string in present page's url this:

var url = window.location; var param = /\?provider=/i; if (url.search(param) != -1) {     alert('it exist'); } else     alert('it not exist'); 

it works when manually define url variable so

var url = 'http://google.com?provider=' 

but when try grab dynamically in above script doesn't work, there way make work?

you want href property on location object, this:

var url = window.location.href; var param = /\?provider=/i; if (url.search(param) != -1) {     alert('it exist'); } else     alert('it not exist'); 

location isn't string, it's object, , doesn't have .search() method, .href string does.


Comments

Popular posts from this blog

ruby - When to use an ORM (Sequel, Datamapper, AR, etc.) vs. pure SQL for querying -

php - PHPDoc: @return void necessary? -

c++ - Convert big endian to little endian when reading from a binary file -