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

2019-08-13 23:53发布

I have an ASP.NET web site. There's an ASP.NET menu on the Masterpage. I want to to hide the menu if the current page is the login page. My login page is Login.aspx. Here's the code how I make the menu invisible/visible:

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

But when I deploy it on the IIS, the url does not include the page name when the web site is opened for the first time, thus the menu becomes visible.How do I determine the current page in this case?

4条回答
萌系小妹纸
2楼-- · 2019-08-14 00:29

If you want to do that in javascript, you can do that as below

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

So,

if( url_length==3 || pathArray[pathArray.length-1]=="login.aspx")
{
    $('#mainmenu').hide();
}
 else
 {
     $('#mainmenu').show();
 }

Hope this will help you.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-08-14 00:36

If the url doesn't change when on the login screen, your only options are to check the page's content, or to set a cookie: Make the server set something like a "pageIsLogin=true" cookie and check if document.cookie has that.

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

(Don't forget to unset that cookie on other pages)

Or, like my fist suggestion, check if the page contains a login-specific element:

if(document.getElementById("loginSpecificField")){
    //Login-specific settings here.
}else...
查看更多
smile是对你的礼貌
4楼-- · 2019-08-14 00:37

You should be doing it in server side IMO. anyway suppose your web app address is http://yourdomain.com/app/ and your login page is the default page. then it will be displaied to user even if he is not typing http://yourdomain.com/app/login.aspx so all we need to check is that if our address is ending with yourdomain.com/app/ or not. if so we will hide the menu.

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();
查看更多
乱世女痞
5楼-- · 2019-08-14 00:45

Supply a special variable on each "page". It's the classic go-to for this scenario. It is commonly used to allow a scripted, included menu system differentiate between any and all pages, and provide functionality on that basis such as highlighting, removing the link, etc. The way it works is to have a specific variable set on each page, which is then tested by the menu system and acted on accordingly.

The same variable can be reused for a variety of reasons, e.g. testing whether a specific function should be available, inclusion of page elements, etc.

查看更多
登录 后发表回答