pstein Posted February 5, 2021 Share Posted February 5, 2021 I am searching for a JavaScript function which let me check if the URL of the current webpage (e.g. "https:\\www.mydomain.com\foobar\blah\") contains a certain pattern (e.g. "foobar\")? If yes, further, following commands should be executed. Otherwise the processing should continue after If statement. Is there such a function? Link to comment Share on other sites More sharing options...
Ingolme Posted February 5, 2021 Share Posted February 5, 2021 You can get the URL from location.href and use indexOf() to search for a string inside the URL. If you need a more complex search, you can use regular expressions. Link to comment Share on other sites More sharing options...
pstein Posted February 9, 2021 Author Share Posted February 9, 2021 (edited) Ok, thank you. However if ((location.href).indexOf("foobar\") == -1) { alert(" not found"); } is not working Whats wrong? Edited February 9, 2021 by pstein Link to comment Share on other sites More sharing options...
dsonesuk Posted February 9, 2021 Share Posted February 9, 2021 You have the wrong slashes in url. And because you have that wrong slash before the quote, that usually escapes the quote from being treated as javascript quote at the end to a quote within a string. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now