行的脚本代码不适用于方法 - javascript

我的.aspx页(HTML标记)中有一个脚本:

<div id="alert">
    <asp:Label ID="lblAlert" style="font-size: xx-large" runat="server"></asp:Label>
</div>
<!-- /.alert -->

<script>

    function AutoHideAlert(v) {
        var al = document.getElementById('<%=lblAlert.ClientID%>');

        al.innerText = v;

        $("#alert").fadeTo(3500, 500).slideUp(1500, function () {
            $("#alert").slideUp(500);
        });
    }

</script>

我正在使用AutoHideAlert(v)在aspx.cs文件(代码隐藏)中调用RegisterStartupScript函数,并且在RegisterStartupScript方法中添加了ShowMessage

private void ShowMessage(string msg)
{
    ScriptManager.RegisterStartupScript(this, GetType(), null, "AutoHideAlert('"+msg+"');", true);
}

问题是当我调用包含脚本代码行的ShowMessage方法时不起作用。但是,当我运行脚本代码行时,它就会起作用;问题是为什么它不与ShowMessage运行?

编辑1:通过@ M4N的注释,我已经通过将第三个参数设置为"Alert Message"进行了尝试,但仍然无法正常工作。

private void ShowMessage(string msg)
{
    ScriptManager.RegisterStartupScript(this, GetType(), "Alert Message", "AutoHideAlert('"+msg+"');", true);
}

参考方案

我想ScriptManager.RegisterStartupScript在生成带方法的脚本之前先生成脚本。由于JS在浏览器读取时执行,因此在执行时AutoHideAlert(如果尚不存在)
尝试使用$(document).ready是你有JQuery

ScriptManager.RegisterStartupScript(this, GetType(), "Alert Message",
     "$(document).ready(function(){ AutoHideAlert('"+msg+"'); }", true);

document.addEventListener('DOMContentLoaded'没有JQuery

ScriptManager.RegisterStartupScript(this, GetType(), "Alert Message",
     "document.addEventListener('DOMContentLoaded', 
                                function(){ AutoHideAlert('"+msg+"'); })", true);

javascript popupwindow之后的行如何工作? - javascript

好的,我有一个来自后面代码的方法,可以创建一个popupwindow。然后有一行代码要在那之后执行,我想知道那行代码何时执行,是在使用popupwindow之后执行还是在创建popupwindow之后执行?例如:void exPopupWindowMethod() { string scr = "window.open('exampleP…

Javascript-Urls的奇怪字符串比较行为 - javascript

最近,在编写我无法理解的javascript时遇到了字符串比较的问题。我从完全相同的网址创建了两个字符串,当我比较它们时返回false,但是在重新分配相同的字符串后,比较返回true。这是我的示例:var str1 = "http://google.com/"; var str2 = "http://google.com‏/&#…

Javascript-从当前网址中删除查询字符串 - javascript

单击提交按钮后,我需要从网址中删除查询字符串值。我可以用jQuery做到这一点吗?当前网址:siteUrl/page.php?key=value 页面提交后:siteUrl/page.php 实际上,我已经从另一个带有查询字符串的页面着陆到当前页面。我需要在页面首次加载时查询字符串值以预填充一些详细信息。但是,一旦我提交了表格,我就需要删除查询字符串值。我已…

在JavaScript函数中转义引号 - javascript

我正在尝试将变量传递给javascript函数。根据用户的选择,它可以是文本或图像。这里已经讨论了类似的问题,但我无法解决。在php中,我这样编码:if ($choice == 1) { $img = '<img src = "../folder/'.$_SESSION["img"].'�…

如何调用超链接单击中包含单引号的消息的JavaScript警报? - javascript

我陷入了javascript问题。我正在使用C#编写可以调用javascript来显示警报消息的超链接。请参阅下面的代码以了解它是如何完成的:首先,这是C#从服务器端编写的代码://Server side code string myHyperlink = "<a href='#' onclick=\"alert…