Why can not I append script to DOM on mobile browsers. It looks like manipulating the dom is completely different in mobile browsers, but actually have no idea in what sense. I ve not got smth like that before, it was always ok. It also looks like the problem is not so actual, as I can not find any proper info about it here on stackoverflow. All suggestions to similar questions are mostly about syntax errors or even smth meaningless according to my case. In my code there are no mistakes, and this makes debugging really hard. Does anyone has an idea or maybe has encountered with smth like this?
function thumb_handler(data) {
console.log(data);
let icons = document.querySelectorAll('.messenger-wrap a span');
for (var i = 0; i < icons.length; i++) {
if (icons[i].getAttribute('class') == data.class) icons[i].style.background = 'url(' + data.img + ')';
}
}
function requestServerCall(cls, img) {
var head = document.head;
var script = document.createElement('script');
script.type = "text/javascript";
script.text = "alert('voila!');"
script.setAttribute('src', 'http://local/general.php?do=load_icons&callback=thumb_handler&class=' + cls + '&img=' + img);
head.appendChild(script);
// head.removeChild(script);
}
function icon_loader() {
let icons = document.querySelectorAll('.messenger-wrap a span');
for (var i = 0; i < icons.length; i++) {
let img = icons[i].getAttribute('data-img'),
cls = icons[i].getAttribute('class');
requestServerCall(cls, img);
}
}
icon_loader();
It is completely ok on any desktop, but not on mobiles. It just does not appear in the dom tree.
The issue was that I was trying to make the code work on a server without checking twice if any stuff for local environment is still present.
script.setAttribute('src', 'http://local/general.php?do=load_icons&callback=thumb_handler&class=' + cls + '&img=' + img);
url obviously points to a localhost domain... And should be as following (as absolute):
script.setAttribute('src', 'http://example.com/general.php?do=load_icons&callback=thumb_handler&class=' + cls + '&img=' + img);
or just (as relative):
script.setAttribute('src', 'general.php?do=load_icons&callback=thumb_handler&class=' + cls + '&img=' + img);