Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if user is visiting pure root url

I'd like to run a js script if a user is visiting my site's root url. I am using this code:

<script type="text/javascript">
    if(location.pathname == "/") {
        window.open ('#107','_self',false);
    }
</script>

However http://example.com/?foo=bar#hash will also run the script, since the pathname excludes the query string and location hash.

It currently behaves like this:

http://example/                  Root
              /?querystring      Root
              /#hash             Root
              /page              Not root

I want it to behave like this:

http://example/                  Root
              /?querystring      Not root
              /#hash             Not root
              /page              Not root

Any suggestions? Thanks.

like image 648
Dan Dinu Avatar asked Dec 11 '25 17:12

Dan Dinu


1 Answers

Use window.location.href instead

like image 142
Umur Kontacı Avatar answered Dec 14 '25 18:12

Umur Kontacı