admin管理员组

文章数量:1410717

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

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

Share asked Feb 1, 2011 at 9:18 Christian P.Christian P. 4,8847 gold badges55 silver badges70 bronze badges 1
  • Would the better solution not be to only set the value if it currently is unset? Or, even better, to create the HTML with the proper preselected value in the first place and not have any JavaScript touch the setting at all? – Christopher Creutzig Commented Feb 1, 2011 at 9:33
Add a ment  | 

3 Answers 3

Reset to default 5

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?

本文标签: javascriptCheck if page was fetched using POSTStack Overflow