Javascript 在母版页上定义JS函数,但用内容页修改它?

Javascript 在母版页上定义JS函数,但用内容页修改它?,javascript,jquery,asp.net,Javascript,Jquery,Asp.net,我在内容页上有一个JS函数,它需要一些特定于该内容页的控件ID 内容页 function invokeAjaxrequest() { $find("<%= RadAjax1.ClientID%>").ajaxRequestWithTarget("<%= RadAjax1.UniqueID%>", "reload"); } function closeAlert(oWnd) { var arg = oWnd

我在内容页上有一个JS函数,它需要一些特定于该内容页的控件ID

内容页

 function invokeAjaxrequest() {
            $find("<%= RadAjax1.ClientID%>").ajaxRequestWithTarget("<%= RadAjax1.UniqueID%>", "reload");
        }
function closeAlert(oWnd) {
            var arg = oWnd.argument;
            if (arg) {
                addAlert(arg);
                invokeAjaxrequest();
            }
        }

如何定义invokeajaxrequest函数,或者检查它是否存在于母版页中,以便在内容页未定义它时,不会导致异常?

您可以检查函数是否存在于母版页中

    if (typeof func === "function")
要添加到代码中,请执行以下操作:

    function closeAlert(oWnd) {
        var arg = oWnd.argument;
        if (arg) {
            addAlert(arg);
            if (typeof invokeAjaxrequest === "function") { 
                invokeAjaxrequest();
            }
            // misread - you _don't_ want an error if it doesn't exist
            // so just do nothing here (don't have the else)
            //else {
            //    alert("invokeAjaxrequest must be defined on each page");
            //}
        }
    }

作为一个额外的,我会考虑使用某种形式的回调,而不是检查FUNC是否存在。

< p>您可以检查函数是否存在

    if (typeof func === "function")
要添加到代码中,请执行以下操作:

    function closeAlert(oWnd) {
        var arg = oWnd.argument;
        if (arg) {
            addAlert(arg);
            if (typeof invokeAjaxrequest === "function") { 
                invokeAjaxrequest();
            }
            // misread - you _don't_ want an error if it doesn't exist
            // so just do nothing here (don't have the else)
            //else {
            //    alert("invokeAjaxrequest must be defined on each page");
            //}
        }
    }

作为一个额外的,我会考虑使用某种形式的回调,而不是检查FUNC是否存在。

< P>这是一个可能的解决方案

母版页-定义默认函数行为

<!-- this will be overridden in the content page -->
<asp:ContentPlaceHolder ID="PageScriptSection" >
    function invokeAjaxrequest() {
    }
</asp:ContentPlaceHolder>

函数invokeAjaxrequest(){
}
这确保了函数始终存在。要覆盖它,请在内容页中定义它

内容页-必要时覆盖特定页面的脚本

<asp:Content ID="CustomScript" ContentPlaceHolder="PageScriptSection">
    function invokeAjaxrequest() {
         $find("<%= RadAjax1.ClientID%>").ajaxRequestWithTarget("<%= RadAjax1.UniqueID%>", "reload");
    }
</asp:Content>

函数invokeAjaxrequest(){
$find(“”).ajaxRequestWithTarget(“,“reload”);
}

这里有一个可能的解决方案

母版页-定义默认函数行为

<!-- this will be overridden in the content page -->
<asp:ContentPlaceHolder ID="PageScriptSection" >
    function invokeAjaxrequest() {
    }
</asp:ContentPlaceHolder>

函数invokeAjaxrequest(){
}
这确保了函数始终存在。要覆盖它,请在内容页中定义它

内容页-必要时覆盖特定页面的脚本

<asp:Content ID="CustomScript" ContentPlaceHolder="PageScriptSection">
    function invokeAjaxrequest() {
         $find("<%= RadAjax1.ClientID%>").ajaxRequestWithTarget("<%= RadAjax1.UniqueID%>", "reload");
    }
</asp:Content>

函数invokeAjaxrequest(){
$find(“”).ajaxRequestWithTarget(“,“reload”);
}

所有常规函数都是窗口对象中的方法

假设您的内容页加载在父iframe中

从父级(主级)中的内容窗口进行检查:

从父级(主级)中的内容窗口定义:


如果使用跨域策略,请小心,否则会出现“保护”错误,您将无法从父级访问content vice verca。

所有常规功能都是窗口对象中的方法

假设您的内容页加载在父iframe中

从父级(主级)中的内容窗口进行检查:

从父级(主级)中的内容窗口定义:


如果您使用跨域策略,请务必小心,否则会出现“保护”错误,您将无法从家长处访问content vice verca。

如何访问您的内容页?如何访问您的内容页?只要母版页中的代码在内容页中的代码之前(我忘了使用
ContentPlaceHolder
),这是一个优雅的解决方案。我认为这似乎比@freedomn-m更优雅,但两者都可以。谢谢!只要母版页中的代码在内容页中的代码之前(我忘了使用
ContentPlaceHolder
),这是一个优雅的解决方案。我认为这似乎比@freedomn-m更优雅,但两者都可以。谢谢!