cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
GLMSDev
Helper II
Helper II

Reference JS file in solution's HTML in Web Resource

Hi

 

I have web resources as shown in attached file. I have HTML file which is referenced in JS file which is in same place (as shown in Image). If I refernce JS file in HTML Like https://<domain>//WebResources/grid_debugCode However, If i try and do relative path. Somehow it's not working. 

 

How do I give relative path ?

1 ACCEPTED SOLUTION

Accepted Solutions

double slashes i.e. // can do very weird things to some web servers.

 

I suspect if you used 

<head>
<script src='/WebResources/grid_debugCode'></script>
</head>
<body onload="myFunction()">
<h1>Hello World!</h1>
</body>

it will work. 

---
If this post has answered your question please consider it for "Accept as Solution" or if it has been helpful give it a "Thumbs Up".

View solution in original post

9 REPLIES 9
a33ik
MVP

Can you please provide url of your html and part of html where you referene your js webresource?

HTML file

https://<domain>//WebResources/grid_debugHTML1

 

JS file

https://<domain>//WebResources/grid_debugCode

 

Below is my HTML Code

 


<script src='https://<domain>//WebResources/grid_debugCode'></script>
</head>
<body onload="myFunction()">
<h1>Hello World!</h1>
</body>

double slashes i.e. // can do very weird things to some web servers.

 

I suspect if you used 

<head>
<script src='/WebResources/grid_debugCode'></script>
</head>
<body onload="myFunction()">
<h1>Hello World!</h1>
</body>

it will work. 

---
If this post has answered your question please consider it for "Accept as Solution" or if it has been helpful give it a "Thumbs Up".

Following should work fine:

<script src='grid_debugCode'></script>

@ben-thompson  with all the respect this is not the perfect solution. It will work fine for IFD and for Online but it will not work for On-Prem because of orgname prepending.

true, but the OP does state in his first post that relative paths weren't working which is why I suggested the absolute path approach

 

In the back of my mind I seem to remember that the best approach for relative paths is actually 

 

<script src='./grid_debugCode'></script>

 

 (note the additional ./) but I can't remember why I remember that and it could be from the recent past or it could be from NCSA httpd days (yes I am that old) but I think it's far more recent than that.

---
If this post has answered your question please consider it for "Accept as Solution" or if it has been helpful give it a "Thumbs Up".

I can be wrong but 

 

<script src='./grid_debugCode'></script>

 

and

 

<script src='grid_debugCode'></script>

 

 are equal.

Here is what google says to me regarding relative paths - https://www.w3schools.com/html/html_filepaths.asp:

PathDescription
<img src="picture.jpg">picture.jpg is located in the same folder as the current page

@a33ik This is completely off-topic so I'm happy to move to linkedin messaging or elsewhere but there was definitely a scenario where 

 

./grid_debugCode

 

 worked but 

grid_debugCode

didn't and for me to remember it, it must have really, really annoyed me at the time.

 

Looking at an PCF component I have open on the other screen I wonder if it was to do with file systems as the manifest file has everything as "css/file.css" but the index.ts is "./generated/Manifests" and as I said it really wouldn't surprise me if this is something from 15+ or even 25 years ago but I'm sure it's more recent than that and connected with a CRM project.

---
If this post has answered your question please consider it for "Accept as Solution" or if it has been helpful give it a "Thumbs Up".
EricRegnier
Super User
Super User

Hi @GLMSDev,

Can you get the full URL instead with method getClientUrl (https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-utili...) and load your JS file at run-time with that base URL? Your web resource will be embedded on the form so to reference the Xrm client scripts you'll need window.parent.Xrm.Utility.getGlobalContext(). This way it will be sure to work regardless if on-prem or cloud.

 

 

 

Helpful resources

Announcements
PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

May UG Leader Call Carousel 768x460.png

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

Users online (1,586)