开发者

Check if page was fetched using POST

Is there any way, using javascript, to check if the page was the result of a POST or GET request?

The reason is that I have a sharepoint search page where we have a custom javascript inserted to automatically choose a specific value. For new searches this is not开发者_C百科 a problem, but as the next/previous results pages are implemented using postback, the value gets reset and the next pages get a different result if the value was changed.

Example: Default value is "Choose" (= no value). Our script sets it to "Value 1". A new user goes to the search page, changes it to "Value 2" and searches. When he gets the results back, our script sets it back to "Value 1" and when he clicks "Next" he gets results for page 2 for a search for "Value 1" instead of "Value 2".

The solution would be to check if the page was submitted using POST, and only reset the value if it was not.


No, JavaScript can't detect such a thing.

What you can do is inject some "flag" into JS from within the code behind:

void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
        Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "post_back_flag", "var _postBack = true;", true);
    }
}

Then check for that flag in your JS code.


You can use the Page.IsPostBack property on the server side to avoid generating the reset part of your client script during postbacks:

protected void Page_PreRender(object sender, EventArgs e)
{
    if (!IsPostBack) {
        // Render the script that resets the selection to "Value 1".
    }

    // Render the rest of the script.
}


I think the simple answer would be that if it was a POST request you can't access the variables via JavaScript. Can you not check the URL for a query string and if there is one then it was a GET request?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜