I want to load content from multiple other pages into one page, and organize the content based on their data attributes. I am able to load the content but am struggling to get the data to load in specified parts of the page (based on the data attribute within a div tag).
I'm totally new to jquery so any help would be appreciated.
Script:
<?xml version="1.0" encoding="utf-8"?>
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd">
<head>
<link href=".../Resources/Stylesheets/Styles.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(function () {
var includes = $('.loadContent'); // get all div with loadContent class
// loop on all divs
$(includes).each(function(index, element) {
var include = $(this).data('include'), mrConditions = $(this).data('mc-conditions');
//console.log('data-include : ' + include + ' - data-mc-conditions : ' + mrConditions);
//console.log(element);
// set the url of your file
var fileURL = '/Test1/Content/AZTopics/' + include + '.aspx';
// get data for each associated files
$.ajax({
url: fileURL, // replace with fileURL var
async: false, // asynchronous request
cache: false, // force the browser to not make cache of the retrieved data
dataType: "text", // jQuery will infer this, but you can set explicitly
success: function( data, textStatus, jqXHR ) {
var resourceContent = data; // can be a global variable too...
//$(element).html(resourceContent);
if ($(resourceContent).find('MadCap[conditions="Facing.UtilityApps"]')) {
$('.resultContent').html('<div>' + $(resourceContent).find('MadCap[conditions="Facing.UtilityApps"]').text() + '</div>');
} else if ($(resourceContent).find('MadCap[conditions="Facing.HardwareApps"]')) {
$('.resultContent').html('<div>' + $(resourceContent).find('MadCap[conditions="Facing.HardwareApps"]').text() + '</div>');
}
}, error: function (data) {
console.log(data.status + ':' + data.statusText,data.responseText);
}
});
})
})
</script>
</head>
<body>
<h1>UtilityApps</h1>
<div data-include="A" data-mc-conditions="UtilityApps" class="loadContent">
</div>
<div data-include="B" data-mc-conditions="UtilityApps" class="loadContent">
</div>
<h1>HardwareApps</h1>
<div data-include="A" data-mc-conditions="HardwareApps" class="loadContent">
</div>
<div data-include="B" data-mc-conditions="HardwareApps" class="loadContent">
</div>
<br>
</br>
<div class="resultContent">
</div>
</body>
</html>
Contents of A.htm:
<?xml version="1.0" encoding="utf-8"?>
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" xml:lang="en-us">
<head><title></title>
<link href=".../Resources/Stylesheets/Styles.css" rel="stylesheet" />
</head>
<body>
<h1>A</h1>
<h3 MadCap:conditions="Facing.UtilityApps">Example one</h3>
<h3 MadCap:conditions="Facing.UtilityApps">Example two</h3>
<h3 MadCap:conditions="Facing.FirmwareApps">Example three</h3>
<h3 MadCap:conditions="Facing.HardwareApps">Example four</h3>
<h3>Example five</h3>
</body>
</html>
Contents of B.htm:
<?xml version="1.0" encoding="utf-8"?>
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" xml:lang="en-us">
<head><title></title>
<link href=".../Resources/Stylesheets/Styles.css" rel="stylesheet" />
</head>
<body>
<h1>B</h1>
<h3 MadCap:conditions="Facing.HardwareApps">Example six</h3>
<h3 MadCap:conditions="Facing.HardwareApps">Example seven</h3>
<h3 MadCap:conditions="Facing.FirmwareApps">Example eight</h3>
<h3 MadCap:conditions="Facing.UtilityApps">Example nine</h3>
<h3>Example ten</h3>
</body>
</html>