javascript - How do I get the current page name if it's not displayed in the URL when that page is the default page for

I have an ASP.NET web site. There's an ASP.NET menu on the Masterpage. I want to to hide the menu

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 bees visible.How do I determine the current page in this case?

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 bees visible.How do I determine the current page in this case?

Share Improve this question edited Dec 13, 2012 at 9:25 Arash Milani 6,3082 gold badges45 silver badges49 bronze badges asked Dec 13, 2012 at 8:43 Mikayil AbdullayevMikayil Abdullayev 12.4k27 gold badges129 silver badges214 bronze badges 6
  • 1 Check for a element that only appears on the login page. – Cerbrus Commented Dec 13, 2012 at 8:45
  • You are doing this in clientside. It's better to do it in server side. not using javascript – Arash Milani Commented Dec 13, 2012 at 8:46
  • @CEbrus, that trick came to my mind too. But I find it a bit not smart – Mikayil Abdullayev Commented Dec 13, 2012 at 8:47
  • @ArashMilani, I've tried to do it on the server side before, that's what I too wanted at first.But the Menu is placed in the MasterPage. How do I control it? Wait a minute, maybe a Session variable? SOmething like a flag? I'll check it. – Mikayil Abdullayev Commented Dec 13, 2012 at 8:50
  • 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. – Cerbrus Commented Dec 13, 2012 at 8:51
 |  Show 1 more ment

4 Answers 4

Reset to default 2

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.
var url_length = pathArray.length;
//if url is http://www.example., then url_length will have 3
//if url is http://www.example./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.

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

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

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

Supply a special variable on each "page". It's the classic go-to for this scenario. It is monly 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745430350a4627361.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信