我如何在JavaScript中检查的IsPostBack? [英] How can I check for IsPostBack in JavaScript?
问题描述
我需要运行JavaScript函数的onLoad(),但只有这样做,如果网页加载在第一时间(即不是回发的结果)。
I need to run a JavaScript function onLoad(), but only do it if the page loaded the first time (i.e. is not the result of a postback).
基本上,我需要在JavaScript中检查的IsPostBack。
Basically, I need to check for IsPostBack in JavaScript.
感谢您。
推荐答案
服务器端,写:
if(IsPostBack)
{
// NOTE: the following uses an overload of RegisterClientScriptBlock()
// that will surround our string with the needed script tags
ClientScript.RegisterClientScriptBlock(GetType(), "IsPostBack", "var isPostBack = true;", true);
}
然后,在你的脚本为运行在onLoad,检查该变量是否存在:
Then, in your script which runs for the onLoad, check for the existence of that variable:
if(isPostBack) {
// do your thing
}
您并不真正需要,否则设置变量,像乔纳森的解决方案。客户端if语句将正常工作,因为的IsPostBack变量将是不确定的,这在计算结果为假的if语句。
You don't really need to set the variable otherwise, like Jonathan's solution. The client-side if statement will work fine because the "isPostBack" variable will be undefined, which evaluates as false in that if statement.
这篇关于我如何在JavaScript中检查的IsPostBack?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!