Stop all code and continue as warning () in JS

Let me explain: when you call alert () in JS, all the code below the warning will stop, and when you click OK, the code will return to work.

I create my own custom alert using this code:

function cAlert() { var bOn; this.show = function (content) { bOn = true; document.write('<div id="turnOffLight"></div><div id="cAlertBox"><div id="cAlertImageBox"><img style="position: absolute; top: 54%; left: 50%; margin-top: -32px; margin-left: -32px;" src="Images/Vectors/1429744831_678136-shield-warning-64.png" alt=""/> </div><div id="cAlertContentBox"> </div><div id="cAlertButtonBox"><input id="cAlertButtonStyle" type="button" value="OK" onclick="cAlert.ok()" /></div></div>'); $("#cAlertContentBox").html(content); $("#cAlertBox").show(); $("#turnOffLight").fadeIn("fast"); var div = document.getElementById('cAlertBox').offsetWidth; var res = -Math.abs(div / 2); document.getElementById('cAlertBox').style.marginTop = res + "px"; }; this.ok = function () { $("#cAlertBox").hide(); $("#turnOffLight").fadeOut("medium"); bOn = false; }; } 

and on other pages:

 <head> <script src="Scripts/jQuery_1.11.2.js" type="text/javascript"></script> <script src="Scripts/cAlertScript.js" type="text/javascript"></script> <script type="text/javascript">var cAlert = new cAlert();</script> </head> 

but I have a problem on the login page after calling cAlert (), I call this:

 echo "<script>javascript:history.go(-1)</script>"; 

to return to the last page, when I used the notification (), only after the user clicks in order, the browser will return to the last page, but with cAlert the browser will return to the last page.

I want the user to press the OK button so that the code continues as a warning ().

+7
javascript alert
source share
2 answers

Change cAlert.show(content) to cAlert.show(content, success) . Change cAlert.ok() to cAlert.ok(' + success + ') . Change this.ok = function () to this.ok = function (success) and finally add success(); to the end of the ok() function.

This will trigger success when the button in cAlert is pressed.

Instead of calling cAlert.show("text") you need to call cAlert.show("text", function() { javascript:history.go(-1); } ) .


So your code should be:

 function cAlert() { var bOn; this.show = function (content, success) { bOn = true; document.write('<div id="turnOffLight"></div><div id="cAlertBox"><div id="cAlertImageBox"><img style="position: absolute; top: 54%; left: 50%; margin-top: -32px; margin-left: -32px;" src="Images/Vectors/1429744831_678136-shield-warning-64.png" alt=""/> </div><div id="cAlertContentBox"> </div><div id="cAlertButtonBox"><input id="cAlertButtonStyle" type="button" value="OK" onclick="cAlert.ok(' + success + ')" /></div></div>'); $("#cAlertContentBox").html(content); $("#cAlertBox").show(); $("#turnOffLight").fadeIn("fast"); var div = document.getElementById('cAlertBox').offsetWidth; var res = -Math.abs(div / 2); document.getElementById('cAlertBox').style.marginTop = res + "px"; }; this.ok = function (success) { $("#cAlertBox").hide(); $("#turnOffLight").fadeOut("medium"); bOn = false; success(); }; } 

and

 <head> <script src="Scripts/jQuery_1.11.2.js" type="text/javascript"></script> <script src="Scripts/cAlertScript.js" type="text/javascript"></script> <script type="text/javascript"> var cAlert = new cAlert(); cAlert.show("text", function() { javascript:history.go(-1); } ); </script> </head> 
+2
source share

Since you want the page to return only after you click OK, put the function call or code back into the cAlert.ok() function. Thus, the page will only return after clicking the "OK" button, since this function is called only when the user clicks the "OK" button.

 this.ok = function () { $("#cAlertBox").hide(); $("#turnOffLight").fadeOut("medium"); bOn = false; window.history.go(-1); }; 
0
source share

All Articles