[英]Defining a JS function on a master page but modifying it with a content page?
我在内容页面上有一个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是否存在。
这是一个可能的解决方案
母版页 -定义默认功能行为
<!-- this will be overridden in the content page -->
<asp:ContentPlaceHolder ID="PageScriptSection" >
function invokeAjaxrequest() {
}
</asp:ContentPlaceHolder>
这样可以确保该功能始终存在。 要覆盖它,请在内容页面中对其进行定义
内容页面 -必要时覆盖特定页面的脚本
<asp:Content ID="CustomScript" ContentPlaceHolder="PageScriptSection">
function invokeAjaxrequest() {
$find("<%= RadAjax1.ClientID%>").ajaxRequestWithTarget("<%= RadAjax1.UniqueID%>", "reload");
}
</asp:Content>
所有常规功能都是窗口对象中的方法。
假设您的内容页面已加载到父iframe中,
从父级(母版)的内容窗口中检查:
if (window.parent.invokeAjaxrequest== "function") { }
从父(母版)的内容窗口中定义:
window.parent.invokeAjaxrequest = function() { .... };
如果您使用跨域策略,请谨慎使用,否则会出现“保护”错误,并且您将无法从父级访问内容反之亦然。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.