javascript 如何检查用户是否正在访问站点的根 url?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8100515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if the user is visiting the site's root url?
提问by Kamyar
I'd like to fire up some script if the user is visiting my site's root url.
如果用户正在访问我网站的根 url,我想启动一些脚本。
For example, I want to do something in my view when the user is visiting
比如我想在用户访问的时候在我的视图中做一些事情
www.example.com
example.com
www.example.com/
http://www.example.com
http://example.com
www.example.com
example.com
www.example.com/
http://www.example.com
http://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.
而不是为www.example.com/anything else
.
在 ASP.NET MVC 3 [Razor] 网站和 javascript 的视图页面中进行检查的最安全方法是什么?另外,有什么方法可以仅使用 javascript 找出答案吗?
谢谢你。
回答by Rob W
The easiest JavaScript method is:
最简单的 JavaScript 方法是:
var is_root = location.pathname == "/"; //Equals true if we're at the root
Even http://example.com/?foo=bar#hash
will produce the right result, since the pathname excludes the query string and location hash.
Evenhttp://example.com/?foo=bar#hash
会产生正确的结果,因为路径名不包括查询字符串和位置哈希。
Have a look:
看一看:
http://anything-but-a-slash/ Root
/?querystring Root
/#hash Root
/page Not root
If you have index file(s) at your root folder, have a look at the following example:
如果您的根文件夹中有索引文件,请查看以下示例:
var is_root =/^\/(?:|index\.aspx?)$/i.test(location.pathname);
The previous line is using a regular expression. Special characters have to be escaped, the /i
postfix makes the pattern case-insensitive. If you want the case to match, omit the i
flag.
前一行使用了正则表达式。特殊字符必须被转义,/i
后缀使模式不区分大小写。如果您希望大小写匹配,请省略i
标志。
The same regular expression presented graphically:
相同的正则表达式以图形方式呈现:
回答by Stephan
Try this more robust regular expression:
试试这个更强大的正则表达式:
Regex
正则表达式
var isRoot =/^(\/|\/index\.asp|\/index\.aspx)$/i.test(location.pathname);
Description
描述
Demo
演示
回答by Ronnie Royston
Testing for pathname of "/" failed when I ran my app in debug/preview mode from my online IDE. In other words it normally is served with a clean url (no .html
) except when I serve it from Cloud9 in which case it is served with index.html
.
当我从我的在线 IDE 以调试/预览模式运行我的应用程序时,测试“/”的路径名失败。换句话说,它通常使用干净的 url (no .html
) 提供,除非我从 Cloud9 提供它,在这种情况下它使用index.html
.
So, this is working for me:
所以,这对我有用:
if(window.location.pathname.length == 1 || window.location.pathname.length == 0 || window.location.pathname === "/index.html" || window.location.pathname === "/index"){
//I'm at the app root
}
if(window.location.pathname.length == 1 || window.location.pathname.length == 0 || window.location.pathname === "/index.html" || window.location.pathname === "/index"){
//I'm at the app root
}
回答by Mic
With a regular expression like:
使用正则表达式,如:
(/^https?\:\/\/[^\/]+\/?$/).test(window.location.href)
(/^https?\:\/\/[^\/]+\/?$/).test(window.location.href)
回答by Ortiga
I think
我认为
if(window.location == window.location.hostname) {
//is root
}
Edit:Do not use this, see comment
编辑:不要使用这个,见评论