Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.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函数_Javascript_Asp.net_Code Behind - Fatal编程技术网

从代码隐藏错误调用javascript函数

从代码隐藏错误调用javascript函数,javascript,asp.net,code-behind,Javascript,Asp.net,Code Behind,我试图调用从aspx页面加载到母版页中的一个javascript函数 以下是我当前使用的代码: ScriptManager.RegisterStartupScript(Me.Page, GetType(String), "showNotifier", "showNotifier(3000,'green','test');", True) javascript代码如下所示: function showNotifier(delayTime, color, theMsg) { var the

我试图调用从aspx页面加载到母版页中的一个javascript函数

以下是我当前使用的代码:

ScriptManager.RegisterStartupScript(Me.Page, GetType(String), "showNotifier", "showNotifier(3000,'green','test');", True)
javascript代码如下所示:

function showNotifier(delayTime, color, theMsg) {
    var theDivName = '#Notifier';
    e.preventDefault();
    $(theDivName).css("background", color);
    $(theDivName).text(theMsg);
    $(theDivName).animate({ top: 0 }, 300, null);
    $(theDivName).css("position", "fixed");

    //Hide the bar
    $notifyTimer = setTimeout(function () {
        $(theDivName).animate({ top: -100 }, 1500, function () {
            $(theDivName).css("position", "absolute");
        });
    }, delayTime);
});
这就是在我的代码隐藏中调用它的地方:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        ScriptManager.RegisterStartupScript(Me.Page, GetType(String), "showNotifier", "showNotifier(3000,'green','test');", True)
    End If
End Sub
当我运行页面并加载该aspx页面时,它会显示以下错误:

Microsoft JScript runtime error: 'showNotifier' is undefined.

什么会导致这种情况?

请改用
RegisterClientScriptBlock

并将调用包装为$(function(){…}):

更新:因此您的VB代码如下所示:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "showNotifier", ";$(function() {showNotifier(3000,'green','test');});", True)
    End If
End Sub
更新2您的js函数有一些语法错误:

function showNotifier(delayTime, color, theMsg) {
    var theDivName = '#Notifier';
    // 'e' is not declared -- e.preventDefault();
    $(theDivName).css("background", color);
    $(theDivName).text(theMsg);
    $(theDivName).animate({ top: 0 }, 300, null);
    $(theDivName).css("position", "fixed");

    //Hide the bar
    $notifyTimer = setTimeout(function () {
        $(theDivName).animate({ top: -100 }, 1500, function () {
            $(theDivName).css("position", "absolute");
        });
    }, delayTime);
}// ); - this should not be here

showNotifier住在哪里?代码是否在页面上?@epascarello:showNotifier是加载在母版页内的javascript文件。如果说RegisterClient不是System.Web.UI.ScriptManager的成员,请使用
RegisterClient ScriptBlock
。我已经更新了应答器,但仍然显示了Microsoft JScript运行时错误的原始错误:“showNotifier”未定义。嗯。。。你能打开生成的HTML并看到函数是在那里定义的吗?另外,在函数的末尾有一个括号,应该把它去掉。那时候我就知道了。这是因为javascript中的错误。胡说八道,希望微软能告诉我,而不是给我那个一般性的错误!
function showNotifier(delayTime, color, theMsg) {
    var theDivName = '#Notifier';
    // 'e' is not declared -- e.preventDefault();
    $(theDivName).css("background", color);
    $(theDivName).text(theMsg);
    $(theDivName).animate({ top: 0 }, 300, null);
    $(theDivName).css("position", "fixed");

    //Hide the bar
    $notifyTimer = setTimeout(function () {
        $(theDivName).animate({ top: -100 }, 1500, function () {
            $(theDivName).css("position", "absolute");
        });
    }, delayTime);
}// ); - this should not be here