Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 停止所有代码,然后继续,就像JS中的alert()一样_Javascript_Alert - Fatal编程技术网

Javascript 停止所有代码,然后继续,就像JS中的alert()一样

Javascript 停止所有代码,然后继续,就像JS中的alert()一样,javascript,alert,Javascript,Alert,让我解释一下:当您在JS中调用alert()时,警报下面的所有代码都将停止,当您单击Ok时,代码返回work 我使用以下代码制作自己的自定义警报: function cAlert() { var bOn; this.show = function (content) { bOn = true; document.write('<div id="turnOffLight"></div><div id="cAlertBox"><

让我解释一下:当您在JS中调用alert()时,警报下面的所有代码都将停止,当您单击Ok时,代码返回work

我使用以下代码制作自己的自定义警报:

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;
};
}
函数cAlert()
{
var bOn;
this.show=函数(内容)
{       
苯教=真;
文件。写(“”);
$(“#cAlertContentBox”).html(内容);
$(“#cAlertBox”).show();
$(“#关闭灯”).fadeIn(“快速”);
var div=document.getElementById('cAlertBox').offsetWidth;
var res=-Math.abs(div/2);
document.getElementById('cAlertBox').style.marginTop=res+“px”;
};
this.ok=函数()
{
$(“#cAlertBox”).hide();
$(“#关闭灯”)。淡出(“中等”);
苯教=假;
};
}
在其他页面中:

<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>

var cAlert=新的cAlert();

但我有一个问题,在登录页面中,在调用cAlert()之后,我称之为:

echo "<script>javascript:history.go(-1)</script>";
echo“javascript:history.go(-1)”;
要返回到最后一页,当我使用alert()时,只有在用户单击“确定”后,浏览器才会返回到最后一页,但使用cAlert时,浏览器已经返回到最后一页


我想要的是,用户需要单击“确定”按钮以使代码继续作为警报()。

由于您只希望页面在单击“确定”按钮后返回,请将函数调用或代码放在
cAlert.Ok()函数中返回。这样,页面只会在用户单击OK按钮后返回,因为只有在用户单击OK按钮时才会调用此函数

this.ok = function ()
{
    $("#cAlertBox").hide();
    $("#turnOffLight").fadeOut("medium");
    bOn = false;

    window.history.go(-1);
};
cAlert.show(内容)
更改为
cAlert.show(内容,成功)
。 将
cAlert.ok()
更改为
cAlert.ok(“+success+”)
。 将
this.ok=function()
更改为
this.ok=function(success)
,最后添加
success()
ok()
函数的末尾

单击cAlert中的按钮时,将调用
success

您现在需要调用
cAlert.show(“text”),而不是调用
cAlert.show(“text”,function(){javascript:history.go(-1);})


因此,您的代码现在应该是:

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();
};
}
函数cAlert()
{
var bOn;
this.show=功能(内容、成功)
{       
苯教=真;
文件。写(“”);
$(“#cAlertContentBox”).html(内容);
$(“#cAlertBox”).show();
$(“#关闭灯”).fadeIn(“快速”);
var div=document.getElementById('cAlertBox').offsetWidth;
var res=-Math.abs(div/2);
document.getElementById('cAlertBox').style.marginTop=res+“px”;
};
this.ok=功能(成功)
{
$(“#cAlertBox”).hide();
$(“#关闭灯”)。淡出(“中等”);
苯教=假;
成功();
};
}
以及:


var cAlert=新的cAlert();
show(“text”,function(){javascript:history.go(-1);});

我使用这个:echo“cAlert.show('logadocomsucesso!',function(){javascript:history.go(-1);});”;警报出现了,但当我点击Ok时,什么也没发生。哈哈,我忘记编辑一半的代码,后来才发现它根本不起作用。现在应该可以用了,我已经测试过了。。。因此,查看编辑后的帖子,希望我没有错过任何东西。这对我没有任何帮助,因为我想在其他页面中使用此警报,而不是一直使用,我需要返回到最后一页…除了本机函数
alert()
confirm()
prompt()
,在JS中这是不可能的。只有那些有能力停止执行线程。当然,除非您想编写自己的JavaScript引擎,该引擎也会停止其他函数的执行…:不管怎样,写得好的问题P+1。
<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>