An interesting client-side registration event

        When writing a normal code, I suddenly found a very interesting problem, the client's window.location.href cannot be executed?
        The code is simple. We place a BUTTON control on the page, and then add the client registration code to the Page_Load event of the server:

None.gif if  ( ! Page.IsPostBack)
ExpandedBlockStart.gifContractedBlock.gif
{ this.Button1.Attributes["onclick"="javascript:return ConfirmSelect();";} dot.gif
InBlock.gif    
 
ExpandedBlockEnd.gif
In javascript in client side:
None.gif function ComfirmSelect()
ExpandedBlockStart.gifContractedBlock.gif
{     window.location.href ="abc.aspx";//return false;} dot.gif
InBlock.gif
 
InBlock.gif    

ExpandedBlockEnd.gif

    According to the assumed logic, window.location should be executed directly from the client without returning to the server. But actually? The client-side event is not executed at all, and the server-side Button1_Click event is still executed.
    Let's rewrite the client-side Javascript again and see what happens:
None.gif function  ConfirmSelect()
ExpandedBlockStart.gifContractedBlock.gif
{     alert( " First execution! " );     window.location.href = " abc.aspx " ;     alert( " Second execution! " ); // return false; } dot.gif
InBlock.gif

InBlock.gif

InBlock.gif

InBlock.gif    

ExpandedBlockEnd.gif
    We will notice that when the first alert() is executed, the progress bar in the status bar is not loaded, and then the second alert() will be displayed while the progress bar is loaded. At this point we can notice that window.location has been overwritten by events on the server side. That is to say, the client event registered on the server side is invalid for window.location.
    Now let's uncomment the return false line and see how it is handled. In this case, only the first alert is handled logically, and then the page is passed to the server once, and the second alert() is executed again, and finally I go to the page I want. (in reverse order?)

       It's not clear why this is happening, hope to get an answer.

Reprinted in: https://www.cnblogs.com/badwood/archive/2006/11/19/564966.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324075792&siteId=291194637