Now that my window.onbeforeunload process is working correctly, I don't want it to fire when the user uses the buttons I have provided:
<asp:Button ID="btnSaveResponses" runat="server" Text="Save" onclick="btnSaveResponses_Click" />
<asp:Button ID="btnFinished" runat="server" Text="Finished" onclick="btnFinshed_Click" CausesValidation="False" />
If the user clicks either of these, I don't want my window.onbeforeunload procedure to fire.
This is that procedure:
<script language="javascript" type="text/javascript">
window.onbeforeunload = function (e) {
var element1 = document.getElementById('btnFinished');
if (element1 != null) {
//code to set the value variable.
document.getElementById('btnFinished').click();
}
else {
alert("element is null");
}
return false;
};
</script>
I added these 2 functions to the bottom of the page:
<script language="javascript" type="text/javascript">
function setDoNotFire
{
var doNotFire=true;
};
</script>
<script language="javascript" type="text/javascript">
function Fire
{
var doNotFire=false;
};
</script>
Plus this:
<body onload ="Fire()">
And this:
<asp:Button ID="btnSaveResponses" runat="server" Text="Save" onclick="btnSaveResponses_Click" OnClientClick ="setDoNotFire()"/>
<asp:Button ID="btnFinished" runat="server" Text="Finished" onclick="btnFinshed_Click" CausesValidation="False" OnClientClick ="setDoNotFire()"/>
And finally:
<script language="javascript" type="text/javascript">
window.onbeforeunload = function (e) {
if (doNotFire!=true) {
var element1 = document.getElementById('btnFinished');
if (element1 != null) {
//code to set the value variable.
document.getElementById('btnFinished').click();
}
else {
alert("element is null");
}
return false;
}
};
</script>
It didn't work at all. It no longer does anything, so I am guessing it cannot find the variable doNotFire.
There is no need to override it, just make the existing one more flexible. Declare a global variable doNotFire =false
. When button is clicked, swith variable to true. In you onBeforeUnload
code, check that doNotFire
is false.
<html>
<head>
<script type="text/javascript">
doNotFire = false;
function doSomething(){
if (!doNotFire){
....
}
</script>
</head>
<body onbeforeunload="doSomething();">
...
<asp:Button ID="btnSaveResponses" runat="server" Text="Save" onclick="doNotFiretrue;btnSaveResponses_Click();" />
<asp:Button ID="btnFinished" runat="server" Text="Finished" onclick="doNotFiretrue; btnFinshed_Click();" CausesValidation="False" />
</body>
</html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.