My current project involves modifying an HTML web resource for use in Dynamics 365. I need to replace a static URL with a dynamic value obtained via Javascript, specifically:
var URL = Xrm.Page.context.getClientUrl();
There are multiple instances within the resource where this replacement needs to happen, such as in this CSS snippet:
.aftertd {
background: transparent url("REPLACETHIS/_imgs/imagestrips/grid_ctrl_imgs.png?ver=-1792584992") no-repeat scroll -171px -19px;
height: 14px;
overflow: hidden;
width: 4px;
cursor: col-resize;
position: absolute;
right: 0;
}
<th>
<span ng-click="sort('createdon')" class="tdSpanTh">
Date
<img ng-show="sortKey=='createdon'" alt="The data is sorted in ascending order on this column" src="REPLACETHIS/_imgs/imagestrips/transparent_spacer.gif" ng-class="{'ms-crm-List-Sortable ms-crm-Image-Margin ms-crm-ImageStrip-sorting_up':!reverse,'ms-crm-List-Sortable ms-crm-Image-Margin ms-crm-ImageStrip-sorting_down':reverse}" style="visibility:visible;" title="The data is sorted in ascending order on this column">
</span>
<span class="aftertd pull-right"></span>
</th>
The goal is to substitute the static string REPLACETHIS (in the src attribute above) and the CSS property value with the URL variable's dynamic content.
I've attempted one approach like this:
document.body.innerHTML = document.body.innerHTML.replace(/REPLACETHIS/g, URL);
However, I'm curious if there might be a more efficient solution given that REPLACETHIS appears multiple times in the HTML file.