【问题标题】:Error: The value of the property 'warn_redirect' is null or undefined, not a Function object错误:属性“warn_redirect”的值为 null 或未定义,而不是函数对象
【发布时间】:2013-11-26 16:15:32
【问题描述】:

我的一个 ASP.NET 应用程序 (VB) 中有一个页面,该页面依赖于实时的 3rd 方服务,如果发现此服务已关闭,我将使用 jQuery 显示覆盖。我一直在对应用程序中的其他警告使用相同的方法-与此不同的是,它需要在用户单击警告弹出窗口上的按钮以删除覆盖层后进行重定向。出于某种原因,我收到错误“错误:属性 'warn_redirect' 的值为 null 或未定义,而不是函数对象。”

非常感谢任何帮助!我的代码如下:

jQuery

function warn_redirect(msg, title, nextpage) {
    // show modal div
    //alert(obj.id);
    $("html").css("overflow", "hidden");
    $("body").append("<div id='popup_overlay'></div><div id='popup_window'></div>");
    $("#popup_overlay").addClass("popup_overlayBG");
    $("#popup_overlay").fadeIn("slow");

    // build warning box
    $("#popup_window").append("<h1>" + title + "</h1>");
    $("#popup_window").append("<p id='popup_message'><center>" + msg + "</center></p>");
    $("#popup_window").append("<div class='buttons'><center><button id='continue' class='positive' type='submit'>OK</button></center></div>");

    // attach action to button
    $("#continue").click(popup_remove_redirect(nextpage));

    // display warning window
    popup_position(400, 300);
    $("#popup_window").css({ display: "block" }); //for safari using css instead of show
    $("#continue").focus();
    $("#continue").blur();
}

function popup_remove_redirect(nextpage) {
$("#popup_window").fadeOut("fast", function () { $('#popup_window,#popup_overlay').trigger("unload").unbind().remove(); });
$("body", "html").css({ height: "auto", width: "auto" });
$("html").css("overflow", "");
window.location.href = nextpage;

}

这是VB.NET的调用代码:

If Status = "DOWN" Then
            Dim clsUtility As New Utility
            clsUtility.Emailer("email@company.com", "email@company.com", "", "", "The Service Is Down!", "Please investigate")
            Dim ScriptString As String = "<script language='javascript'>"
            ScriptString += "warn_redirect('Some warning message.', 'Warning', 'AnotherPage.aspx');"
            ScriptString += "</script>"
            ClientScript.RegisterStartupScript(Me.GetType, "warnscript", ScriptString)
            'Response.Redirect("AnotherPage.aspx?ID=" & Session("SelKey") & "&UN=" & Header1.UserNumber) //this didn't work either
        End If

【问题讨论】:

    标签: javascript jquery asp.net vb.net


    【解决方案1】:

    试试这个,

    If Status = "DOWN" Then
                Dim clsUtility As New Utility
                clsUtility.Emailer("email@company.com", "email@company.com", "", "", "The Service Is Down!", "Please investigate")
                Dim ScriptString As String = "<script language='javascript'>"
                ScriptString += "window.onload = function(){warn_redirect('Some warning message.', 'Warning', 'AnotherPage.aspx');};"
                ScriptString += "</script>"
                ClientScript.RegisterStartupScript(Me.GetType, "warnscript", ScriptString)
                'Response.Redirect("AnotherPage.aspx?ID=" & Session("SelKey") & "&UN=" & Header1.UserNumber) //this didn't work either
            End If
    

    由于已经包含了 JQuery,你也可以使用下面的语句来代替。

    ScriptString += "$(function(){warn_redirect('Some warning message.', 'Warning', 'AnotherPage.aspx');});"
    

    【讨论】:

    • 感谢您的建议!我确实尝试了两种方法,但仍然遇到与以前相同的错误。
    • 您也可以在上面的代码前面加上另一个RegisterStartupScript 来加载JavaScript 文件。 ClientScript.RegisterStartupScript(Me.GetType, "warnscriptfile", "&lt;script src='myfile.js' /&gt;")
    【解决方案2】:

    这是因为你的脚本定义是在 StartupScript 被调用之后加载的。

    您必须在页面中的 StartupScripts 之前包含您的 .js 文件。在浏览器中查看“源代码”,你会看到一堆这样的脚本标签:

    <script src="/ScriptResource.axd?d=eoXLgy ... 847d" type="text/javascript"></script>
    

    这些标签是作为启动脚本插入的脚本。确保在 .aspx(或 .master)之前插入脚本。也许您必须将它们包含在 head 标记中。

    顺便说一句,创建一个全局函数,比如你的 warn_redirect,这不是一个好习惯。你至少应该这样命名它:

    window.MyFunctions = {
       warn_redirect = function (msg, title, nextpage) { /* your code */ }
    }
    

    然后这样称呼它:MyFunctions.warn_redirect

    这样,您可以更好地组织它,并避免与其他同名函数发生冲突。

    【讨论】:

    • 非常感谢您的反馈。我确实将 jQuery 库文件和我的 jQuery 源文件的引用移到了头部,但仍然得到同样的错误。
    • 然后你应该在浏览器开发者工具中打开浏览器javascript控制台(Chrome或者IE或者Firefox+Firebug中的F12),刷新页面看看有没有错误。 (比起 IE,我更喜欢 Chrome 或 Firefox+Firebug)。有时,另一个 .js 文件中的错误会导致以下文件的代码无法正常工作。
    • 另外,在控制台中,让我知道您在键入 warn_redirect 时会得到什么结果。不明确的?还要别的吗?。也可以尝试调试定义函数的js文件(例如在Chrome控制台中,在Sources中查找文件(窗口左上角的右箭头按钮),并设置断点(点击边距) 得到 suer 它被执行而没有错误。
    猜你喜欢
    • 2014-07-10
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2016-08-28
    • 1970-01-01
    相关资源
    最近更新 更多