00 Votes

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;

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?

0Best Answer0 Votes

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:

var s = window.location.toString();

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

ReplyPositive Negative

Related Topics

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.


Ask your own question or write your own article on askingbox.com. That’s how it’s done.