javascript - 如果当前页面名称未显示在 URL 中,而该页面是 Web 应用程序的默认页面,我该如何获取该页面名称?

标签 javascript asp.net url

我有一个 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/

相关文章:

javascript - 使用 Jquery 选择父 div 内的孙子

javascript - jquery javascript 关于加载变量范围

javascript - 旧的(以前的 html5)ajax 文件上传框架如何工作?

asp.net - SqlDataSource 在变量后面插入代码

.net - LINQ to SQL 并发冲突 - 看起来像是带有正确行版本控制的干净附加

ios - 如何通过 iOS 中的 json 链接打开在模拟器或设备中可用但在应用商店中不可用的应用程序?

javascript - Jquery - 清除或删除 URL 参数

javascript - 根据先前的单选选项禁用单选按钮

asp.net - 关于webservice异常处理的建议

javascript - 相对 url 在 JavaScript (element.src) 中不起作用