I'm using an ajax request to display a loop of drupal posts.
$( document ).ready(function() {
$.ajax({
type:"GET",
url:"https://www.nba.com/api/1.1/json/?type=photo_gallery&tags=community&size=4",
success: function(data) {
let galleriesWrapper = document.getElementById("wrapper--community-galleries");
function create(tagName, props) {
return Object.assign(document.createElement(tagName), (props || {}));
}
function ac(p, c) {
if (c) p.appendChild(c);
return p;
}
for (var i=0; i< data.content.length; i++) {
var link = create("a", {
className: "wrapper--single-gallery",
href: data.content[i].url,
style: "background-image:url(" + data.content[i].images.large + ")"
});
var videoTitle = create("div", {
className: "single-gallery--title",
src: data.content[i].title
});
videoTitle.textContent = data.content[i].title;
ac(galleriesWrapper, ac(link, videoTitle));
}
},
dataType: "jsonp",
});
});
I'm pulling the post's image from the .json file and injecting it as the css background-image for that particular item in the loop.
This all works perfectly fine in Chrome and Firefox but fails in IE.
I'm using the helper function "create" which uses obect.assign to easily create and append DOM elements.
My first failure it IE was because I needed an object.asign polyfill. No items were showing up.
The Polyfill:
if (typeof Object.assign != 'function') {
Object.assign = function(target) {
'use strict';
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object');
}
target = Object(target);
for (var index = 1; index < arguments.length; index++) {
var source = arguments[index];
if (source != null) {
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
}
return target;
};
}
Once I added this to the top of my JS, the loop items get displayed in IE, but the code I am using to inject the background-image style inline fails (I see an item with the proper post title from the ajax request, but the background is blank).
EDIT: Seems the issue is with trying to append the style property. When I use this:
var link = create("a", {
className: "wrapper--single-gallery",
href: data.content[i].url,
style: "background-image:url(" + data.content[i].images.large + ")"
});
The link is created with the class name and the href, but the style property is not appended.
Thinking it had something to do with how I spliced in the url from the json request data, I tried just using the style "color:red". This created the same result of not appending the style tag to the link.
JS FIDDLE EXAMPLE: https://jsfiddle.net/nolaandy/w3gfawcg/