Skip to content Skip to sidebar Skip to footer

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?

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 cod

Solution 1:

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.comvar 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.

Solution 2:

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();

Solution 3:

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...

Solution 4:

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.

Post a Comment for "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?"