How do I get the current page name if it's not

2019-10-17 12:01发布

我有一个ASP.NET web站点。 有一个在母版的ASP.NET菜单。 我想隐藏菜单,如果当前页面的登录页面。 我的登录页面是为Login.aspx。 下面的代码如何使菜单可见/可见:

var pathname = window.location.pathname;
if (pathname.toLowerCase().indexOf("login.aspx") > 0)
    $('#mainmenu').hide();
else
    $('#mainmenu').show();

但是,当我将它部署在IIS,URL没有网站被打开,第一次的时候,这样的菜单变得visible.How我决定在这种情况下,当前页面包括页面的名字吗?

Answer 1:

你应该在服务器端IMO那样做。 反正假设你的web应用程序的地址是http://yourdomain.com/app/和你的登录页面是默认的页面。 那么它将被displaied用户即使他不打字http://yourdomain.com/app/login.aspx所以我们需要检查的是,如果我们的地址与结束yourdomain.com/app/与否。 如果是这样,我们将隐藏菜单。

var pathname = window.location.pathname;
var appDomainEndding = 'yourdomain.com/app/'
if (pathname.toLowerCase().indexOf("login.aspx") > -1 || 
    pathname.indexOf(appDomainEndding, pathname.length - appDomainEndding.length) > -1)
$('#mainmenu').hide();
else
$('#mainmenu').show();


Answer 2:

如果你想要做的是,在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();
 }

希望这会帮助你。



Answer 3:

如果网址不改变时,在登录屏幕上,你唯一的选择是检查页面的内容,或者设置cookie:使服务器设置有点像"pageIsLogin=true" cookie,并检查document.cookie有。

if(~document.cookie.indexOf("pageIsLogin=true")){
    //Login-specific settings here.
}else...

(不要忘了取消设置上的其他网页那个cookie)

或者,像我的拳头的建议,检查页面包含一个特定的登录元素:

if(document.getElementById("loginSpecificField")){
    //Login-specific settings here.
}else...


Answer 4:

供应每“页”上的特殊变量 。 这是典型的去到了这个场景。 它通常用来允许脚本,包括菜单系统中的任何和所有页面区分,并在此基础如高亮,删除的链接,等等。它的工作方式是让每个页面上的特定变量集上提供的功能,然后通过菜单系统,并进行相应起作用。

相同的变量可以被重复用于多种原因,例如,测试特定的功能是否可用,包括页面元素等。



文章来源: How do I get the current page name if it's not displayed in the URL when that page is the default page for a web app?