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
3 Answers
Reset to default 5No, 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
版权声明:本文标题:javascript - Check if page was fetched using POST - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745016324a2637863.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论