2

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?

3
  • @eicto And how exactly would you get the JavaScript into the server if it's not a web resource? (I've got more than the basics of JS, I'm just not sure how to refer a function in an other web resource. And frankly, I'm not sure that you do neither. My code works - I just wish to refactor it.) Commented Dec 8, 2012 at 13:48
  • 2
    @eicto Not to burst your bubble but the OP did have the tag of CRM Dynamics. I've checked your page and I'm sure that you're a great JS programmer but I think you're missing that MS has managed to make things hrmp... different, when it comes to putting JS into CRM. If you don't get the importance of web resource in CRM, you shouldn't get too cocky criticizing him/her. It is stated in the comment that it's a matter of refactoring a working code. That implies that the basic understanding of JS is ensured. And frankly, It's not that complicated to get JS working on a basic level. Commented Dec 8, 2012 at 14:00
  • yes, i just wanted to show OP a way where he will see simple things in complex problems. I not great JS programmer :) and i mostly not programmer at all, my main job is system administration :) Commented Dec 8, 2012 at 14:23

3 Answers 3

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();
Sign up to request clarification or add additional context in comments.

Comments

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.

  1. Contact Microsoft. You've just found a serious bug.
  2. Watch out. There will be pigs flying very soon.
  3. 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.)

1 Comment

No need to duck. There won't be any airborne pigs any time soon. I actually missed on two of the items listed. Thanks to both of you!
1

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?");

13 Comments

ok tell me one thing, the JS is loading in the iframe and functionis defined in the html containing the iframe ?
@AndreasJohansson: If I have understood your question properly, check my edited answer.
Ah, I was unclear. Sorry for that. The IFRAME contains a web resource (HTML document) and it's in that document that I have a script tag executing some stuff. Besides that I've also uploaded an other web resource to my CRM that is a pure JavaScript. It's that script I'd like to execute a function from.
I'll try that right away. However, I'm not sure how how the server will understand what I mean if I don't specify which file the test function resides in. What if I have two JS web resources and each contains a definition for a function called Boom()? In C#, I'd go something like Package1.Boom() and Package2.Boom() but how do I do that in JavaScript?! They can't simply overwrite each other, can they?
He added the IFRAME component into CRM. It's actually quite different from normal web development. One doesn't get to see the code for creation of an IFRAME (I know, I know - it's really weird) when working with a Dynamics Server. Basically, one only gets to see the contents of the HTML put into the frame. And we're not allowed to manipulate the DOM. (Shocker, right?) However, I see that you still managed to get him a really good suggestion (parent.window.functionName reference). Good work and +1 for that!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.