This question already has an answer here:
这个问题已经有了答案:
- How to detect/track postback in javascript? 10 answers
- 如何在javascript中检测/跟踪回发?10个答案
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函数onLoad(),但只有在第一次加载页面时才执行(也就是说,不是回发的结果)。
Basically, I need to check for IsPostBack in JavaScript.
基本上,我需要在JavaScript中检查IsPostBack。
Thank you.
谢谢你!
10 个解决方案
#1
65
Server-side, write:
服务器端,写:
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);
}
Then, in your script which runs for the onLoad, check for the existence of that variable:
然后,在运行onLoad的脚本中,检查该变量是否存在:
if(isPostBack) {
// do your thing
}
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.
你不需要设置变量,比如Jonathan的解决方案。客户端if语句将工作得很好,因为“isPostBack”变量将是未定义的,在if语句中计算为false。
#2
26
There is an even easier way that does not involve writing anything in the code behind: Just add this line to your javascript:
还有一种更简单的方法,不需要在后面的代码中编写任何内容:只需将这一行添加到javascript:
if(<%=(Not Page.IsPostBack).ToString().ToLower()%>){//Your JavaScript goodies here}
or
或
if(<%=(Page.IsPostBack).ToString().ToLower()%>){//Your JavaScript goodies here}
#3
9
hi try the following ...
嗨,试试下面的……
function pageLoad (sender, args) {
alert (args._isPartialLoad);
}
the result is a Boolean
结果是一个布尔值
#4
4
The solution didn't work for me, I had to adapt it:
解决方案对我不起作用,我不得不去适应它:
protected void Page_Load(object sender, EventArgs e)
{
string script;
if (IsPostBack)
{
script = "var isPostBack = true;";
}
else
{
script = "var isPostBack = false;";
}
Page.ClientScript.RegisterStartupScript(GetType(), "IsPostBack", script, true);
}
Hope this helps.
希望这个有帮助。
#5
2
You could put a hidden input on the page, and after the page loads, give it a value. Then you can check that field, if it was in the post data, it's a postback, otherwise it is not.
您可以在页面上放置一个隐藏的输入,在页面加载之后,给它一个值。然后你可以检查那个字段,如果它在post数据中,它是一个回发,否则它不是。
There were two solutions that used server side code (ASP.NET specific) posted as responses. I think it is worth pointing out that this solution is technology agnostic since it uses client side features only, which are available in all major browsers.
有两个解决方案使用服务器端代码(ASP)。NET specific)作为回复发布。我认为值得指出的是,这种解决方案是技术无关的,因为它只使用客户端特性,在所有主流浏览器中都是可用的。
#6
2
Try this, in this JS we can check if it is post back or not and accordingly do operations in the respective loops.
试试这个,在这个JS中,我们可以检查它是否返回,并相应地在各自的循环中执行操作。
window.onload = isPostBack;
function isPostBack() {
if (!document.getElementById('clientSideIsPostBack')) {
return false;
}
if (document.getElementById('clientSideIsPostBack').value == 'Y') {
***// DO ALL POST BACK RELATED WORK HERE***
return true;
}
else {
***// DO ALL INITIAL LOAD RELATED WORK HERE***
return false;
}
}
#7
0
You can create a hidden textbox with a value of 0. Put the onLoad() code in a if block that checks to make sure the hidden text box value is 0. if it is execute the code and set the textbox value to 1.
您可以创建一个值为0的隐藏文本框。将onLoad()代码放入if块中,该块检查以确保隐藏的文本框值为0。如果是执行代码并将textbox值设置为1。
#8
0
Lots of options here.
大量的选项。
For a pure JS solution, have your page submit to itself, but with additional URL parameter (mypage.html?postback=true) - you can then get the page url with window.location.href, and parse that using a split or regex to look for your variable.
对于纯JS解决方案,请将页面提交给它自己,但是要使用额外的URL参数(mypage.html?postback=true)——然后可以使用window.location来获取页面URL。使用split或regex查找您的变量,并对其进行解析。
The much easier one, assuming you sending back to some sort of scripting language to proces the page (php/perl/asp/cf et. al), is to have them echo a line of javascript in the page setting a variable:
更简单的方法是,假设您发送回某种脚本语言以推进页面(php/perl/asp/cf等),让它们在页面中响应一行javascript,设置一个变量:
<html>
<?php
if ($_POST['myVar']) {
//postback
echo '<script>var postingBack = true;</script>';
//Do other processing
} else {
echo '<script>var postingBack = false;</script>'
} ?>
<script>
function myLoader() {
if (postingBack == false) {
//Do stuff
}
}
<body onLoad="myLoader():"> ...
#9
0
Here is one way (put this in Page_Load):
这里有一种方法(将其放入Page_Load):
if (this.IsPostBack)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"PostbackKey","<script type='text/javascript'>var isPostBack = true;</script>");
}
Then just check that variable in the JS.
然后在JS中检查这个变量。
#10
-2
Create a global variable in and apply the value
在其中创建一个全局变量并应用该值
<script>
var isPostBack = <%=Convert.ToString(Page.IsPostBack).ToLower()%>;
</script>
Then you can reference it from elsewhere
然后你可以从其他地方引用它
#1
65
Server-side, write:
服务器端,写:
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);
}
Then, in your script which runs for the onLoad, check for the existence of that variable:
然后,在运行onLoad的脚本中,检查该变量是否存在:
if(isPostBack) {
// do your thing
}
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.
你不需要设置变量,比如Jonathan的解决方案。客户端if语句将工作得很好,因为“isPostBack”变量将是未定义的,在if语句中计算为false。
#2
26
There is an even easier way that does not involve writing anything in the code behind: Just add this line to your javascript:
还有一种更简单的方法,不需要在后面的代码中编写任何内容:只需将这一行添加到javascript:
if(<%=(Not Page.IsPostBack).ToString().ToLower()%>){//Your JavaScript goodies here}
or
或
if(<%=(Page.IsPostBack).ToString().ToLower()%>){//Your JavaScript goodies here}
#3
9
hi try the following ...
嗨,试试下面的……
function pageLoad (sender, args) {
alert (args._isPartialLoad);
}
the result is a Boolean
结果是一个布尔值
#4
4
The solution didn't work for me, I had to adapt it:
解决方案对我不起作用,我不得不去适应它:
protected void Page_Load(object sender, EventArgs e)
{
string script;
if (IsPostBack)
{
script = "var isPostBack = true;";
}
else
{
script = "var isPostBack = false;";
}
Page.ClientScript.RegisterStartupScript(GetType(), "IsPostBack", script, true);
}
Hope this helps.
希望这个有帮助。
#5
2
You could put a hidden input on the page, and after the page loads, give it a value. Then you can check that field, if it was in the post data, it's a postback, otherwise it is not.
您可以在页面上放置一个隐藏的输入,在页面加载之后,给它一个值。然后你可以检查那个字段,如果它在post数据中,它是一个回发,否则它不是。
There were two solutions that used server side code (ASP.NET specific) posted as responses. I think it is worth pointing out that this solution is technology agnostic since it uses client side features only, which are available in all major browsers.
有两个解决方案使用服务器端代码(ASP)。NET specific)作为回复发布。我认为值得指出的是,这种解决方案是技术无关的,因为它只使用客户端特性,在所有主流浏览器中都是可用的。
#6
2
Try this, in this JS we can check if it is post back or not and accordingly do operations in the respective loops.
试试这个,在这个JS中,我们可以检查它是否返回,并相应地在各自的循环中执行操作。
window.onload = isPostBack;
function isPostBack() {
if (!document.getElementById('clientSideIsPostBack')) {
return false;
}
if (document.getElementById('clientSideIsPostBack').value == 'Y') {
***// DO ALL POST BACK RELATED WORK HERE***
return true;
}
else {
***// DO ALL INITIAL LOAD RELATED WORK HERE***
return false;
}
}
#7
0
You can create a hidden textbox with a value of 0. Put the onLoad() code in a if block that checks to make sure the hidden text box value is 0. if it is execute the code and set the textbox value to 1.
您可以创建一个值为0的隐藏文本框。将onLoad()代码放入if块中,该块检查以确保隐藏的文本框值为0。如果是执行代码并将textbox值设置为1。
#8
0
Lots of options here.
大量的选项。
For a pure JS solution, have your page submit to itself, but with additional URL parameter (mypage.html?postback=true) - you can then get the page url with window.location.href, and parse that using a split or regex to look for your variable.
对于纯JS解决方案,请将页面提交给它自己,但是要使用额外的URL参数(mypage.html?postback=true)——然后可以使用window.location来获取页面URL。使用split或regex查找您的变量,并对其进行解析。
The much easier one, assuming you sending back to some sort of scripting language to proces the page (php/perl/asp/cf et. al), is to have them echo a line of javascript in the page setting a variable:
更简单的方法是,假设您发送回某种脚本语言以推进页面(php/perl/asp/cf等),让它们在页面中响应一行javascript,设置一个变量:
<html>
<?php
if ($_POST['myVar']) {
//postback
echo '<script>var postingBack = true;</script>';
//Do other processing
} else {
echo '<script>var postingBack = false;</script>'
} ?>
<script>
function myLoader() {
if (postingBack == false) {
//Do stuff
}
}
<body onLoad="myLoader():"> ...
#9
0
Here is one way (put this in Page_Load):
这里有一种方法(将其放入Page_Load):
if (this.IsPostBack)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"PostbackKey","<script type='text/javascript'>var isPostBack = true;</script>");
}
Then just check that variable in the JS.
然后在JS中检查这个变量。
#10
-2
Create a global variable in and apply the value
在其中创建一个全局变量并应用该值
<script>
var isPostBack = <%=Convert.ToString(Page.IsPostBack).ToLower()%>;
</script>
Then you can reference it from elsewhere
然后你可以从其他地方引用它