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?

Community
  • 1
  • 1
  • @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.) –  Dec 08 '12 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. – Konrad Viltersten Dec 08 '12 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 :) – zb' Dec 08 '12 at 14:23

3 Answers3

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();
cmartin
  • 2,819
  • 1
  • 26
  • 31
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?");
Amogh Talpallikar
  • 12,084
  • 13
  • 79
  • 135
  • ok tell me one thing, the JS is loading in the iframe and functionis defined in the html containing the iframe ? – Amogh Talpallikar Dec 08 '12 at 13:24
  • @AndreasJohansson: If I have understood your question properly, check my edited answer. – Amogh Talpallikar Dec 08 '12 at 13:30
  • 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. –  Dec 08 '12 at 13:31
  • 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? –  Dec 08 '12 at 13:34
  • check this link for implementing namespaces in JS. Its a hack, not an inbuilt feature. http://elegantcode.com/2011/01/26/basic-javascript-part-8-namespaces/ – Amogh Talpallikar Dec 08 '12 at 13:40
  • I think, my second answer would be more suitable, get hold of the iframe by its id and chnage its src, to run the javascript function defined inside it. but that will also work if the iframe is loaded before your script calls the function. – Amogh Talpallikar Dec 08 '12 at 13:42
  • How do I obtain the *id* if the IFRAME? (Sorry if the question is stupid.) –  Dec 08 '12 at 13:49
  • If you have added the iframe in the html document, it should have an attribute called id. – Amogh Talpallikar Dec 08 '12 at 13:52
  • 1
    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! – Konrad Viltersten Dec 08 '12 at 14:07
  • No idea about CRMs at all. have heard lot about SalesForce platform though! He is almost at the mercy of some API from the CRM then. – Amogh Talpallikar Dec 08 '12 at 14:11
  • @KonradViltersten: There should be something.. some class the CRM must be generating for the iframes some id pattern ! – Amogh Talpallikar Dec 08 '12 at 14:17
  • According to the API, one is **not** allowed to manipulate DOM. And we're only allowed to get the data from it using the methods provided by MS. Yes, it's a PIA. :) – Konrad Viltersten Dec 08 '12 at 14:45
  • @KonradViltersten: you have got a good sense of humor! I wish SO had a feature to follow people like its there in quora! – Amogh Talpallikar Dec 09 '12 at 06:27
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.)

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • 1
    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! –  Dec 08 '12 at 14:11