我有一个 ASP.NET 网站。主页上有一个 ASP.NET 菜单。如果当前页面是登录页面,我想隐藏菜单。我的登录页面是 Login.aspx。这是我如何使菜单不可见/可见的代码:
var pathname = window.location.pathname;
if (pathname.toLowerCase().indexOf("login.aspx") > 0)
$('#mainmenu').hide();
else
$('#mainmenu').show();
但是我部署在IIS上,第一次打开网站时,url中没有包含页面名称,所以会出现菜单。这种情况下如何判断当前页面呢?
最佳答案
如果你想在javascript中做到这一点,你可以按照下面的方式做
var pathArray = window.location.pathname.split( '/' );
// assuming the url as http://www.example.com
var url_length = pathArray.length;
//if url is http://www.example.com, then url_length will have 3
//if url is http://www.example.com/login.aspx, then url_length will have 4
所以,
if( url_length==3 || pathArray[pathArray.length-1]=="login.aspx")
{
$('#mainmenu').hide();
}
else
{
$('#mainmenu').show();
}
希望对您有所帮助。
关于javascript - 如果当前页面名称未显示在 URL 中,而该页面是 Web 应用程序的默认页面,我该如何获取该页面名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13856007/