Defined standard interfaces
= Interface {}; Interface.ParentWin = {}; Interface.ChildWin = {}; / * * * page name of the parent standard interface function provided * / Interface.ParentWin.funName = { getDataFun: "getDataFun", // subpage calls to the sub page data interface updateDataFun: "updateDataFun", // sub-page call to submit data to the parent page Interface closeFun: "closeFun" // when sub-pages need to be closed, call the parent page Close window Interface } / * * * parent page is provided to the interface function needs to provide sub-pages * @param childWinId: corresponding to sub-pages to use the interface id, the id id needs to be consistent with the definition of the sub-page * @param functionName: callback function name to be registered the interface name can only be defined in Interface.ParentWin.funName name * @param callbackFun: data sub-page to the parent page callback function update data, the parameters of the interface objects js * / Interface.ParentWin.setFunForChild = function (childWinId, functionName, callbackFun) { IF (comm.isEmpty (childWinId)) { Alert ( "sub-pages not call interface definition object Id" ); return ; } // Save page provided to the parent Interface call total target sub pages IF (comm.isEmpty (window.childCallbackObj)) { window.childCallbackObj = {}; } // with the specified subpage corresponding callback interface object var childCallbackObj = window.childCallbackObj; IF (comm.isEmpty (childCallbackObj [childWinId])) { childCallbackObj [childWinId] = {}; } var childObj =childCallbackObj [childWinId]; IF (! comm.isEmpty (childObj [functionName])) { Alert ( "sub-pages" + childWinId + "interface is required to call exists" + functionName); return ; } // check whether interfaces are registered interface for ( var Pro in Interface.ParentWin.funName) { IF (Interface.ParentWin.funName [Pro] == functionName) { childObj [functionName] = callbackFun; return ; } } Alert ( ); "sub-pages" + childWinId + "required Call Interface not registered:" + functionName + "Check the interface definition statement object.." } / * * * Check whether the specified sub-page call interfaces exist * / Interface. ChildWin.checkValid = function (childWinId, funname) { var parentWin = window.parent; var childCallbackObj = parentWin.childCallbackObj; IF (comm.isEmpty (childWinId)) { Alert ( "! subpage Id call interface definition object can not be null" ); return to false ; } IF (comm.isEmpty (childCallbackObj)) { Alert ( "parent page call interface definition object does not exist" ); return to false ; } var childObj =childCallbackObj [childWinId]; IF (comm.isEmpty (childObj)) { Alert ( "sub-page calls an interface definition object does not exist" ); return to false ; } IF (comm.isEmpty (childObj [funname])) { Alert ( " parent page call interface definition does not exist: "+ funname); return to false ; } return to true ; } / * * * subpages interface function calls the parent page * @childWinId: sub-page page definition itself Id * @funcName: need to call callback function name parameters need to pass: * @params * @Return: If function returning through which the return * / Interface.ChildWin.callBack = function (childWinId, funcName, the params) { IF (! {Interface.ChildWin.checkValid (childWinId, funcName)) return ; } var = parentWin window.parent; var childObj = parentWin.childCallbackObj [childWinId]; return childObj [funcName] .call (parentWin, the params); }
demo
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>父页面</title> </head> <body> <script src="js/common.js"></script> <script> //传给子页面的值 Interface.ParentWin.setFunForChild("data", Interface.ParentWin.funName.getDataFun, function() { return value; }); //获取子页面函数并调用 window.fun; Interface.ParentWin.setFunForChild("test",Interface.ParentWin.funName.updateDataFun,function(param){ fun = param; }); //调用 var val = fun("1111"); console.log(val); </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>子页面</title> </head> <body> <script src="js/common.js"></script> <script> //父页面传入数据 var data = Interface.ChildWin.callBack("data", Interface.ParentWin.funName.getDataFun); console.log(data); //提供给父页面调用的函数 Interface.ChildWin.callBack("test",Interface.ParentWin.funName.updateDataFun,function(data){ alert(data); var str = "xxx"; return str; }); </script> </body> </html>