Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

How to connect JS web-resource to HTML web-resource correctly

Posted on by

Hello everyone.

I need to create html button on some form of 'contact' entity and add to it some logic. Also I need separate all my JS from html. First things first I created html web-resource with this code:

<!DOCTYPE html>
<html>
    <head>
        <script src=""></script>
    </head>
    <body>
        <button onclick="some()">Copy address</button>
    </body>
</html>
.
.
.
After it I wrote this JS with namespaces in separate web-resource:
var DA = DA || {};
DA.CRM = DA.CRM || {};
DA.CRM.Contact = DA.CRM.Contact || {};
DA.CRM.Contact.FormScripts = {
        some: function(xrmformContext){
            window.Xrm = xrm;
            window._formContext = formContext;
        
            alert("hello world");
}
.
.
.
I have two questions:
1. How I should write the path of JS web-resource for <script src=""></script>? I tried this wariants:
- <script src="defaultPublisher_myResource"></script>
- <script src="defaultPublisher_myResource.js"></script>
- <script src="myResource"></script>
- <script src="./WebResources/defaultPublisher_myResource"></script>
- <script src="./WebResources/defaultPublisher_myResource.js"></script>
All of them didn't work 
2. How I should call my method considering that it is inside namespaces? I mean in this row: <button onclick="some()">Copy address</button>. Something like this DA.CRM.Contact.FormScripts.some()? 
  • Charan Raju C R Profile Picture
    Charan Raju C R 3,155 Super User 2024 Season 1 on at
    RE: How to connect JS web-resource to HTML web-resource correctly

    Hi EaredGen,

    Have you managed to add javascript reference in HTML webresource?

  • Suggested answer
    Charan Raju C R Profile Picture
    Charan Raju C R 3,155 Super User 2024 Season 1 on at
    RE: How to connect JS web-resource to HTML web-resource correctly

    Hi EaredGen,

    1. Javascript webresource path reference in HTML is depends on both HTML and javascript webresource path names. This should always be a relative path, if you have maintained a folder structure while creating the webresources.

    For example, 

    If your javascript webresource name is: new_MyJSWebresource and HTML webresource name is: new_MyHTMLWebresource then, your script source in HTML should be: <script src="new_MyJSWebresource"></script>

    If your javascript webresource name is: new_MyJSWebresource.js and HTML webresource name is: new_MyHTMLWebresource then, your script source in HTML should be: <script src="new_MyJSWebresource.js"></script>

    If your javascript webresource name is: new_/Scripts/new_MyJSWebresource.js and HTML webresource name is: new_MyHTMLWebresource then, your script source in HTML should be: <script src="new_/Scripts/MyJSWebresource.js"></script>

    If your javascript webresource name is: new_/Scripts/new_MyJSWebresource.js and HTML webresource name is: new_/Webpages/MyHTMLWebresource then, your script source in HTML should be: <script src="../Scripts/MyJSWebresource.js"></script>

    2. Calling method should be a complete name including namespace. In your case, it should be: DA.CRM.Contact.FormScripts.some()

    I noticed one syntax error in your javascript code. There is a missing closing brace "}" for some: function. Fix this syntax error and try again, this could resolve your issue.

  • necsa Profile Picture
    necsa 3,455 on at
    RE: How to connect JS web-resource to HTML web-resource correctly

    Hi,

    Please get as reference following link:

    vjcity.blogspot.com/search

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans