web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to run a javascript from inside HTML Web resource

(0) ShareShare
ReportReport
Posted on by

Hello,

I need help with the following situation. I have crm 2011. I added an html web resource into the form. 

How can a call the function javascript that is located inside the HTML web resource?

Really appreciate any help. Thanks!

Angel

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: How to run a javascript from inside HTML Web resource

    Hello Angel,

    Can you please explain your scenario a bit?

    I've used a bit other approach but it worked fine for me - I attached handlers from webresources to CRM form - butenko.pro/.../howto-htmljs-webresources

  • Community Member Profile Picture
    on at
    RE: How to run a javascript from inside HTML Web resource

    I want to run this javascript in the onload event. I put this onload="ProductImagesOnload()" in the body tags but it does not work in the crm but I tried opening the .html and it works.

    This is my html web resource:

    <!DOCTYPE html>
    <html>

    <body onload="ProductImagesOnload()">

    <img id="savings" src="" style="padding-right:10px" >
    <img id="creditcard" src="">
    <img id="car" src="">
    <img id="house" src="">


    <button onclick="ProductImagesOnload()">Try it</button>

    <script type="text/javascript">
    function ProductImagesOnload() {


    alert("yes");
    var grid = window.parent.Xrm.Page.ui.controls.get('ProductsImages')._control;

    alert(grid);

    if (grid.get_innerControl() == null) {
    setTimeout(subGridOnload, 1000);
    return;
    }
    else if (grid.get_innerControl()._element.innerText.search("Loading") != -1) {
    setTimeout(subGridOnload, 1000);
    return;
    }

    var ids = grid.get_innerControl().get_allRecordIds();
    alert(ids);

    for(i = 0; i < ids.length; i++) {

    var x = grid.get_innerControl().getCellValue("rst_productclassidcp", ids[i]);


    alert(x);


    if(x == "Deposit"){

    document.getElementById("savings").src = "";
    flag = 1;
    }


    if(x == "Credit"){

    document.getElementById("creditcard").src = "";
    flag1 = 1;
    }


    }

    }
    </script>

    </body>
    </html>

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: How to run a javascript from inside HTML Web resource

    Try to reformat your html using following format:

    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    function ProductImagesOnload() {
    
    alert("yes");
    var grid = window.parent.Xrm.Page.ui.controls.get('ProductsImages')._control;
    alert(grid);
    if (grid.get_innerControl() == null) {
    setTimeout(subGridOnload, 1000);
    return;
    }
    else if (grid.get_innerControl()._element.innerText.search("Loading") != -1) {
    setTimeout(subGridOnload, 1000);
    return;
    }
    var ids = grid.get_innerControl().get_allRecordIds();
    alert(ids);
    for(i = 0; i < ids.length; i++) {
    
    var x = grid.get_innerControl().getCellValue("rst_productclassidcp", ids[i]);
    
    
    alert(x);
    
    if(x == "Deposit"){
    
    document.getElementById("savings").src = "";
    flag = 1;
    }
    
    
    if(x == "Credit"){
    
    document.getElementById("creditcard").src = "";
    flag1 = 1;
    }
    
    
    } 
    
    }
    </script>
    </head>
    <body onload="ProductImagesOnload()">
    <img id="savings" src="" style="padding-right:10px" >
    <img id="creditcard" src="">
    <img id="car" src="">
    <img id="house" src="">
    <button onclick="ProductImagesOnload()">Try it</button>
    </body>
    </html>


  • Community Member Profile Picture
    on at
    RE: How to run a javascript from inside HTML Web resource

    It works thanks but now I'm having trouble to get the subgrid ID and this onload is happening before the subgrid is loaded.

    Is there a way to call the javascript from the form properties?

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: How to run a javascript from inside HTML Web resource

    You can use following approach:

    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    function loadCheck(){
    	if (window.parent &&
    		window.parent.Xrm &&
    		window.parent.Xrm.Page &&
    		window.parent.Xrm.Page.ui &&
    		window.parent.Xrm.Page.ui.controls.get('ProductsImages') != null {
    		ProductImagesOnload();
    	}
    	else {
    		setTimeout(loadCheck, 1000);
    	}
    }
    
    
    function ProductImagesOnload() {
    
    alert("yes");
    var grid = window.parent.Xrm.Page.ui.controls.get('ProductsImages')._control;
    alert(grid);
    if (grid.get_innerControl() == null) {
    setTimeout(subGridOnload, 1000);
    return;
    }
    else if (grid.get_innerControl()._element.innerText.search("Loading") != -1) {
    setTimeout(subGridOnload, 1000);
    return;
    }
    var ids = grid.get_innerControl().get_allRecordIds();
    alert(ids);
    for(i = 0; i < ids.length; i++) {
    
    var x = grid.get_innerControl().getCellValue("rst_productclassidcp", ids[i]);
    
    
    alert(x);
    
    if(x == "Deposit"){
    
    document.getElementById("savings").src = "";
    flag = 1;
    }
    
    
    if(x == "Credit"){
    
    document.getElementById("creditcard").src = "";
    flag1 = 1;
    }
    
    
    } 
    
    }
    </script>
    </head>
    <body onload="loadCheck()">
    <img id="savings" src="" style="padding-right:10px" >
    <img id="creditcard" src="">
    <img id="car" src="">
    <img id="house" src="">
    <button onclick="ProductImagesOnload()">Try it</button>
    </body>
    </html>


    Just curious why don't you want to try to execute code from form webresource?

  • Community Member Profile Picture
    on at
    RE: How to run a javascript from inside HTML Web resource

    Yeah I really would like to call it from the form webresource. How would I do that?

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: How to run a javascript from inside HTML Web resource

    What you need is to create JavaScript webresource, attach to form and use it in OnLoad handler - www.c5insight.com/.../how-to-use-javascript-on-dynamics-crm-201120132015-form.aspx

  • Community Member Profile Picture
    on at
    RE: How to run a javascript from inside HTML Web resource

    But how would I call it from the html? Do I need to put a path?

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: How to run a javascript from inside HTML Web resource

    Question - why do you need to call it from HTML?

  • Community Member Profile Picture
    on at
    RE: How to run a javascript from inside HTML Web resource

    So, I do not need to add any reference of the javascript in the html?

    For example:

    <!DOCTYPE html>

    <html>

    <head>

    <script type="text/javascript" scr=""/>

    </head>

    <body onload="loadCheck()">

    <img id="savings" src="" style='padding-right:10px' >

    <img id="creditcard" src="">

    <img id="car" src="">

    <img id="house" src="">

    <button onclick="ProductImagesOnload()">Try it</button>

    </body>

    </html>

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#2
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans