In the chrome console, I am able to execute the following javascript function found on another domain of mine:
(function() {
var scr = document.createElement('script');
scr.src = 'https://www.myawesomedomain.com/test.js';
document.head.appendChild(scr);
scr.onload = function(){
myfunc();
};
})()
Where test.js has the following code:
let myfunc = function() {
alert ('you are awesome');
}
All good so far!
Now I have another file on the same domain called myjson.json with the following content:
{"message":"you are awesome!"}
and I would like to modify the initial code to extract the previous json value and do a console.log. I tried the following code, however it doesn't work for some reason:
(function() {
var scr = document.createElement('script');
scr.type = 'application/json';
scr.src = 'https://www.myawesomedomain.com/myjson.json';
document.head.appendChild(scr);
scr.onload = function(){
console.log(scr.message);
};
})()
this.QUESTION:
Am I doing something fundamentall wrong in the code above? I have been stuck here for a while sadly.