As we all know, session is running on the server, JavaScript is running on the client, and JavaScript cannot directly run on the server.Code. However, I have recently encountered the following requirement: in a learning system, users cannot open two online examination or online learning windows at the same time. By opening the modal dialog box, the user is indeed prohibited from opening a new window. However, if the user re-opens a new page, a new dialog box can be opened.
So I thought of using cookies and sessions to record the user's exam or learning window, because cookies and sessions have a global effect. However, the disadvantage of cookies is that if the user clears the cookies, the cookies will be lost, so the session will be used. However, what if you exit the window by pressing the close button of the browser? In this way, no background code is executed. So it is easy to think of the Ajax request method. The following code is used:
Check whether the learning page has been opened in the CS file on the online learning page.
1 If (Session [ " Study " ] = Null | Session [ " Study " ] = "" )
2 {
3 Session [ " Study " ] = " True " ;
4 }
5 Else
6 {
7 String Myscript = @ " Alert ('you have opened an online learning window. You cannot open multiple online learning windows at the same time! '); Type = 1; window. Close (); " ;
8 Page. clientscript. registerstartupscript ( This . GetType (), " Myscript " , Myscript, True );
9 }
Note that a javascript code is registered and the window. Close () method is called. In this case, the value of session ["study"] cannot be cleared when the page is closed. Therefore, a type = 1 is added. If it is type = 1, the session value is not cleared. The front-end also declares a type = 0. When type = 0, the user closes the window directly and needs to clear the session value.
1 Function Window. onbeforeunload (){
2 If (Type = 0 ){
3 VaR XMLHTTP = Createxmlhttprequest ();
4 XMLHTTP. Open ( ' Get ' , " Endstudy. aspx " , True );
5 XMLHTTP. Send ( Null );
6 XMLHTTP. onreadystatechange = Function (){
7 If ( 4 = XMLHTTP. readystate ){
8 If ( 200 = XMLHTTP. Status ){
9 }
10 Else {
11 }
12 }
13 }
14 }
15 }
Clear the session value in endstudy. aspx. CS.
1 protected void page_load ( Object sender, eventargs e)
2 {< br> 3 session [ " Study " ] = " " ;< br> 4 }
It seems that there is no problem with this method. If the session value is cleared on the request page before closing the window, the requirements can be met. However, I was depressed for a long time and found that the first time the page was closed, the sesson value was successfully cleared, and the second time the page was closed, I requested endstudy. aspx succeeded and no endstudy was executed. aspx. the page_load function in CS. Therefore, if you want to open the online learning page again, you cannot "open two online learning windows at the same time ".
Later I thought that the dialog box was cached, so I ran the code to clear the session on the online learning page and Its page_load:
1 Response. Buffer = True ;
2 Response. expiresabsolute = System. datetime. Now. addseconds ( - 1 );
3 Response. Expires = 0 ;
4 Response. cachecontrol = " No-Cache " ;
5 Response. addheader ( " Pragma " , " No-Cache " );
But the problem persists. In the end, the session clearing code is added to the page_load of endstudy. aspx. CS to solve the problem.
The final code of page_load in endstudy. aspx. CS is as follows:
1 Protected Void Page_load ( Object Sender, eventargs E)
2 {
3 Response. Buffer = True ;
4 Response. expiresabsolute = System. datetime. Now. addseconds ( - 1 );
5 Response. Expires = 0 ;
6 Response. cachecontrol = " No-Cache " ;
7 Response. addheader ( " Pragma " , " No-Cache " );
8
9 Session [ " Study " ] = "" ;
10 }