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
4 Answers
Reset to default 2If 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条)