I’d like to fire up some script if the user is visiting my site’s root url.
For example, I want to do something in my view when the user is visiting
www.example.comexample.comwww.example.com/http://www.example.comhttp://example.com
… [Various combinations of the above.]
And not for www.example.com/anything else.
What is the safest way to check this in a view page of a ASP.NET MVC 3 [Razor] web site and javascript? Also, is there any way to find out using only javascript?
Thank you.
The easiest JavaScript method is:
Even
http://example.com/?foo=bar#hashwill produce the right result, since the pathname excludes the query string and location hash.Have a look:
If you have index file(s) at your root folder, have a look at the following example:
The previous line is using a regular expression. Special characters have to be escaped, the
/ipostfix makes the pattern case-insensitive. If you want the case to match, omit theiflag.The same regular expression presented graphically: