0

So I'm making this website. There's this one part where I have to make a gallery, and I wanted the image to link to a webpage where the image and the image description will be loaded dynamically.

So to do test this concept I decided to store a js hashmap of all the string values in a separate jscript file, as below.

imageData.js

var map = {};

map["test.jpg"] = "test desc";

And made the browser alert the result as shown below, when an onClick event is triggered.

imageLinkScript.js

function main() {
    getScript('imageData.js', function() {
    alert(map["test.jpg");
    });
}

function getScript(url, callback) {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;

    script.onreadystatechange = callback;
    script.onload = callback;

    document.getElementsByTagName('head')[0].appendChild(script);
}

Additionally I'm using code from this question to fetch the data of the external .js file.

The problem is that when I trigger the onclick, nothing seems to work. If I replace the getScript() with an alert(), it works perfectly, so it's not an error with getting the Listener to work.

I'm thinking it has something to do with the data fetching. Any help is appreciated!!

Community
  • 1
  • 1
user4442318
  • 71
  • 1
  • 8
  • Looks like, everything is fine. You should be able to see result properly. I tried and saw alert showing "test Desc' which is right data. Can you please share more detail? – Nitesh Mar 05 '17 at 12:23
  • I'm an idiot, apparently I forgot to load the script in the HTML... – user4442318 Mar 05 '17 at 13:42

0 Answers0