且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查会话过期或不并重定向用户使用javascript到登录页面

更新时间:2022-10-20 20:49:41

可以使用下面的逻辑做...

写在下面的登录页面code块

 <脚本类型=文/ JavaScript的>
在window.onload =函数()
{
    如果(窗口!= window.top)
    {
        window.top.location =YourLoginPageURL;
        window.close()的;    }
};
< / SCRIPT>

如果登录页面在IFRA打开

(窗口!= window.top)将返回true

希望它能帮助

I am developing web application using ASP.Net MVC. In my application I am using some many ajax calls through out my application to Get/Post data for some functionality. For some functionality I am showing iframed popup. My issue is that when session is expired in my application I am redirecting user to login page but if user try to access functionality that opens in iframed popup instead of redirecting to login page my popup opens with login page inside it otherwise it's working properly. So what is best way to check session is expired or not and redirecting user to login page from inside of iframed popup. The code for opening iframed popup is as follow:

function ShowPopup(Url, DialogTitle, W, H) {

/* Setting for dialog box */
$("#dialog").dialog({
    title: DialogTitle,
    autoOpen: false,
    modal: true,
    resizable: false,
    width: W,
    height: H,
    open: function (ev, ui) {
        /* Setting URL to open in iframe */
        $('#ContentIframe').attr('src', Url);
    }
});

/* Opening dialog box */
$('#dialog').dialog('open');
}

It can be done using following logic...

Write following block of code in your Login Page

 <script type="text/javascript">
window.onload=function()
{
    if (window!=window.top) 
    { 
        window.top.location = "YourLoginPageURL";
        window.close();

    }
};
</script>

(window!=window.top) will return true if Login page is opened in IFra

Hope it helped