簡體   English   中英

如何在asp.net 4.0中檢查瀏覽器的“ mywindow”選項卡是否處於活動狀態?

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

實際上,我正在使用c#4.0在asp.net中創建在線測試/考試的網站。 為了進行考試,用戶需要單擊一個按鈕,這將打開一個帶有JavaScript功能的窗口。

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

}

我想要的是,在進行考試時,如果用戶更改其選項卡或在他/她的PC中打開一個文件夾,則我想關閉窗口,即“ mywindow”。 我知道在asp.net中不可能實現此目標,所以想知道如何在javascript或jquery中實現此目標?

我已經在網上搜索了幾個答案,現在我知道每次加載“ test.aspx”或“ mywindow”頁面時如何調用JavaScript函數。

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

//我需要糾正yourFunction()中的邏輯,以檢查“ mywindow”或“ test.aspx”是否處於活動狀態,如果是,則我將在alert(“ u取消資格”)上顯示一條消息,然后關閉“ test.aspx”

請有人幫我解決這個問題!!! 請...!!

這就是我的方法...我在Chrome / Opera / Firefox / IE中進行了測試...在IE中,它要求允許關閉所有其他自動關閉的窗口。...不確定如何解決目前的IE錯誤。

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

編輯:此腳本放置在最終的頁面上。 另外,我添加了一個form元素只是為了確保在選擇子元素並且我沒有任何問題時不會關閉窗口。

編輯2:IE錯誤是由於javascript無法打開窗口。 如此使用... <a href="#" id="OpenWindow">Link</a>

接着...

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

子窗口上的腳本仍然有效。 同樣,這是使用jQuery作為選擇器來完成的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM