简体   繁体   中英

How to check If “mywindow” tab of the browser is active or not in asp.net 4.0?

Actually i am making website of online test/exam in asp.net using c# 4.0. And for giving exam the user needs to click on a button which will open a window with a JavaScript function.

    function OpenForm() {
 window.open('Test.aspx', 'mywindow',
'fullscreen=yes,titlebar=no,toolbar=no,statusbar=no,menubar=no');

}

And the thing i want is, while a Exam is going on, if user changes its tab or open a folder in his/her pc then i want to close the window ie "mywindow". i know its not possible in asp.net to achieve this so want to know how can i achieve this in javascript or jquery?

i have search out the net for few answers and now i know how to call a JavaScript function every time my "test.aspx" or "mywindow" page loads.

<form id="form1" runat="server">

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager> 
 <script type="text/javascript">   

function endRequestHandler(sender, args) 
{ 
    yourFunction(); 
} 

function yourFunction() 
{ 

    alert("aaa"); 
} 


function pageLoad() 
{ 
    if(!Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) 
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);

} 
</script> 


    <asp:UpdatePanel ID="UpdatePanel2" runat="server">

        <ContentTemplate> 
            <asp:Timer ID="Timer1" runat="server" Interval="6000">

            </asp:Timer> 
        </ContentTemplate> 
    </asp:UpdatePanel> 
</form> 

//i need to right the logic in yourFunction() to check whether "mywindow" or "test.aspx" is active or not if yes then i will display a message on alert("u are disqualified") and then close the "test.aspx"

Please someone help me out with this!!! please...!!

This is how I did it... I tested in Chrome/Opera/Firefox/IE... In IE it asked for permission to close the window on all the others it closed automatically....Not sure how to get around the IE bug at the moment.

<script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
    $(window).focus();
    $(window).blur(function () {
        window.opener.BadStudent();
        window.close();
    });
    $('#close').click(function () {
        window.close();
    });
});
</script>

EDIT: This script is placed on the page that they would end up on. Also I added a form element just to ensure that the window wouldn't close when a child element was selected and I didn't have any issues.

EDIT 2: The IE bug was due to javascript not opening the window. so insted use... <a href="#" id="OpenWindow">Link</a>

and then...

<script type="text/javascript">
        function BadStudent () {
            alert("Your a bad student");
        };
        $(document).ready(function(){
            $('#OpenWindow').click(function(e){
                e.preventDefault();
                Popup = window.open("@Url.Action("test")").focus();
            });
        });
</script>

The script that goes on the child window is still valid. Also this was done using jQuery for the selectors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM