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

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

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -