Search code examples
javascripturldocumentdetection

How can I tell if a user is on index.html?


I use document.URL to detect if a user is on index.html:

if(document.URL.indexOf("index") >-1) return true;

But if the user types "mydomain.com" or "mydomain.com/" then the test returns false.

I could try:

if(document.URL ==="http://myDomain.com") return true;

But I want to use this code on different domains. Any suggestions?


Solution

  • There are so many permutations of URL that could mean that a user is on index.html. Instead could you not put a var within that file:

    <script type="text/javascript">
        on_index = true;
    </script>
    

    Just check if on_index is not undefined and is true. That'll be accurate 100% of the time.