How to detect/track postback in javascript?

后端 未结 11 1322
猫巷女王i
猫巷女王i 2020-12-02 18:51

How to detect/track/check postback in javascript(e.g in asp.net Page.isPostBack())? Any suggestion?

相关标签:
11条回答
  • 2020-12-02 19:44

    on Page_Load on your server-side : The following uses an overload of RegisterClientScriptBlock() that will surround our string with the needed script tags

    Server-Side

    if (Page.IsPostBack){
       ClientScript.RegisterClientScriptBlock(GetType(),
                "IsPostBack", "var isPostBack = true;", true);
    }
    

    Then in your script which runs for the onLoad, check for the existence of that variable.

    if (isPostBack){
       //do something here
    }
    
    0 讨论(0)
  • 2020-12-02 19:45

    This should work for ASP.Net pages without relying on a backend supplied variable/control:

    function isPostBack(frmID) {
        var eventtarget = "";
        var eventargument = "";
    
        if (!!frmID) {
            if (document.forms.length == 0) return false;
    
            sForm = document.forms[0];
        }
        else {
            sForm = document.getElementById(frmID);
    
            if (!sForm) return false;
        }
    
        if (sForm.__EVENTTARGET) eventtarget = sForm.__EVENTTARGET.value;
        else return false;
    
        if (sForm.__EVENTARGUMENT) eventargument = sForm.__EVENTARGUMENT.value;
        else return false;
    
        if (eventtarget != "" || eventargument != "") return true;
        else return false;
    }
    
    0 讨论(0)
  • 2020-12-02 19:47

    As JavaScript shouldn't be written with server-side code, and injecting new elements into the page seems like overkill, it seems to me that the simplest solution is to add [datat-*] attributes to the <head> element:

    In Page_Load:
    Page.Header.Attributes["data-is-postback"] IsPostBack ? "true" : "false";
    

    This can then be accessed as:

    jQuery:
    $('head').data('isPostback');
    
    Vanilla JS:
    document.head.getAttribute('data-is-postback') === 'true';
    

    Of course, if you treat the [data-is-postback] attribute as a boolean attribute, you could alternatively use:

    In Page_Load:
    if (IsPostBack)
    {
        Page.Header.Attributes.Add("data-is-postback", "");
    }
    else
    {
        Page.Header.Attributes.Remove("data-is-postback");
    }
    
    jQuery:
    $('head').is('[data-is-postback]');
    
    Vanilla JS:
    document.head.hasAttribute('data-is-postback')
    
    0 讨论(0)
  • 2020-12-02 19:48

    ASPX:

    <input type="hidden" id="_ispostback" value="<%=Page.IsPostBack.ToString()%>" />
    

    Client-side Script:

    function isPostBack() { //function to check if page is a postback-ed one
      return document.getElementById('_ispostback').value == 'True';
    }
    

    PS: I have not tested it but I've done somthing similar before and it works.

    0 讨论(0)
  • 2020-12-02 19:51

    I have a solution that worked for me.

    // Postback catch
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(function (s, e) {
        alert("post back");
    });
    
    0 讨论(0)
提交回复
热议问题