必须从另一个webresoucre控件访问Webresource控件。使用以下javascript,
var webResource = $(window.parent.Xrm.Page.getControl(webResourceName).getObject().contentWindow.window.document.getElementById(dropDownName));
但是有时它可以正常工作,有时它返回空值。
因此,可以检查webresource是否已加载。
您有很多选择。
我最喜欢的方法是朝另一个方向发展:在您的Web资源中,添加代码以开始在父CRM窗体上执行。您可以使用jQuery的ready方法,或者可以在不涉及jQuery的SO上阅读的许多方法之一。如果您使用的是jQuery,则您的网络资源可能具有以下内容:
$.ready(function() {
window.parent.Xrm.Page.getAttribute('name').setValue('test'); // or whatever your webresource needs to do
});
这个想法是,Web资源准备就绪时会触发一些逻辑,这样您就可以避免必须检测Web资源何时完成加载的麻烦。
我不记得我是否实际尝试过,但是您应该可以使用jQuery的load方法。在CRM表单上的脚本中,执行以下操作:
$('iframe#WebResource_xyz').on('load', function() {
// Here, the 'this' object will refer to the iframe object
this.contentWindow.document.getElementById(dropDownName); // you might have to tweak this slightly, didn't test it
});
这种方法可以满足您的要求,并且是我在开箱即用的表单上使用jQuery之前曾经使用的方法(包括将来可能使用较旧版本的CRM(无法使用jQuery)的读者)。它等待webresource完成加载,然后调用回调。将此函数添加到CRM表单上加载的脚本中:
// Waits for web resource to be ready and then invokes the callback.
// webResourceId: the id of either a web resource or an iframe on the form
// urlCheck: this string will be checked for in the iframe's url to make
// sure it is on the right page. can be any part of the url,
// doesn't have to be the whole thing.
// callback: Called once the iframe is ready. The context of the callback
// method will be set to the iframe's window, so the callback can
// use "this" to refer to the iframe window.
function waitForWebResourceReady(webResourceId, urlCheck, callback) {
var tryCount = arguments[3] || 0;
var control = Xrm.Page.getControl(webResourceId);
if (!control ||
!control.getObject() ||
!control.getObject().contentWindow ||
!control.getObject().contentWindow.location ||
!control.getObject().contentWindow.location.href ||
control.getObject().contentWindow.location.href.indexOf(urlCheck) < 0 ||
control.getObject().readyState !== 'complete') {
if (tryCount > 50) {
console.log("waitForWebResourceReady: " +
"Failed to reach ready state on " + webResourceId);
return;
}
console.log("waitForWebResourceReady: " + webResourceId + " not ready yet");
window.setTimeout(function () {
waitForWebResourceReady(webResourceId, urlCheck, callback, ++tryCount);
}, 20);
return;
}
console.log("waitForWebResourceReady: " + webResourceId + " is ready");
callback.call(control.getObject().contentWindow);
}
然后像这样使用它:
waitForWebResourceReady('WebResource_xyz', 'mycontrol.html', function () {
// In this context, 'this' will refer to the window object of the webresource
var dropdown = this.document.getElementById(dropDownName);
// ....
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句