JavaScript: window.location.indexOf not working
Question by Guest | Last update on 2021-06-13 | Created on 2016-03-27
I would like to check my current URL with JavaScript in order to see whether a specific string is part of the address.
For this, I am using the function indexOf in the following way:
var s = window.location; alert(s.indexOf("localhost"));
The problem is that nothing happens with this code. I even do not get any error message, nothing. What am I doing wrong? In my opinion, I am using the function in the right way, or?
Related Topics
JavaScript: window.location to String
Info | 0 Comments
HTML Form: Redirection depending on Radiobutton or Checkbox State
Tutorial | 0 Comments
jQuery: Scroll up and down with and without JavaScript
Tip | 0 Comments
JavaScript: Catch Submit of Form
Tutorial | 0 Comments
JavaScript: Show and hide HTML area by clicking a link
Tutorial | 0 Comments
JavaScript: Create and Use Arrays
Info | 0 Comments
jQuery: Disable Submit Button if no Checkbox is selected
Tutorial | 7 Comments
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.
The problem with this is that window.location is an object and not a string. And the object window.location does not know the function .indexOf() so that you cannot use it.
Therefore, you have to convert window.location into a string first to make it work. For example like that:
With this approach, it should work.
Here you can find further possibilities for converting window.location to a string.
Last update on 2021-06-13 | Created on 2016-03-27