问题
I've uploaded two web resources, a.HTML and b.JS. In the HTML document I have a section where a script is executed (and it works as supposed to) upload loading into the IFRAME on my form.
Now, because of the size of the code, I feel the need to refactor it and I'd like to move out some of the methods from the script tag of my HTML web resource to a separate JS web resource.
At the first step I've set up the separate JS web resource as follows.
function test() { alert("Success."); }
From the script inside the HTML document I execute the following, getting an error as test seems not to be known to the page.
alert("Get ready for test...");
test();
alert("Did it work?");
I've added the JS web resource to the form and savepublished, of course. There's surprisingly little info on the subject out there. I've found those links but none of them matches exactly what I need and gives me no hint on how to approach the issue at hand.
- This link resembles what I want to achieve but it's about calling JS from JS where both are web resources.
- This link is diagonally opposite to what I want but I don't know how to reverse it from calling HTML from JS whre both are web resources.
What should I correct?
回答1:
Check the following assumptions that you did:
- save the web resource?
- publish all changes?
- add the resource to the frame in the regarded entity?
- name the function hazaa
- call the correct name, i.e.
parent.window.test()?
If yes to all of the above, do three things.
- Contact Microsoft. You've just found a serious bug.
- Watch out. There will be pigs flying very soon.
- Get a coat. It's about to get much colder.
(By that, I mean that you surely have missed on something in the list I've provided and that you need not to contact Microsoft, pigs won't start to fly and the hell won't freeze over.)
回答2:
Calling a function in a javascript webresource, from an HTML webresource in Microsoft Dynamics CRM
CRM 2015
Javascript webresource:
function test() { alert("Success."); }
HTML webresource:
window.parent.test();
CRM 2016
They now load HTML webresources in an Iframe so you can't access external js files, but you can access the XRM object. So you can place the function on this object.
Javascript webresource:
Xrm.Page.test = test;
function test() { alert("Success."); }
HTML webresource:
window.parent.Xrm.Page.test();
回答3:
I think your test() function is in scope of your html document's window. and when you call it from inside the iframe's document, it searches for test() in its scope.
Try out
alert("Get ready for test...");
parent.window.test();
alert("Did it work?");
and in case the test() is defined in the iframe and you are calling it from the HTML document try this.
alert("Get ready for test...");
document.getElementById("iframeId").src ="javascript:test();"
alert("Did it work?");
来源:https://stackoverflow.com/questions/13777660/calling-a-js-function-from-html-iframe-both-web-resources