0

I was just experimenting with some javascript, browsing the web for fast search on the client side. I came accross this link where blogger briefly mentions loading data as an executable script by wrapping it in a callback function.

Even though this might not be the best way to do it, can someone explain how this might actually work?

I have been trying the following code in the head section.

<script id="jsonD" type="application/json" src="/data/catalogEntries.json"></script>

I can see in the dev tools that the file is being transferred. But I can't figure out how to use the data in another script.

I also tried the following at the end of the body section.

       $(document).ready(function () {
            var head = document.getElementsByTagName('head')[0];
            var script = document.createElement('script');
            script.type = 'application/json';
            script.src = '/data/catalogEntries.json';
            head.appendChild(script);
        })

I am also trying to find out how I can measure the time it takes for the browser to parse the data and compare it with a plain use of eval(). The data that I have is more than 4k Kilobytes.

I know I can do this using XMLHttpRequests and I have done this multiple times but this seemed like a much faster way of parsing a large amount of data based on the blog link that I posted earlier.

Any help appreciated.

Thanks, Paras

UPDATE

Here is a plunker

https://plnkr.co/edit/C2N0Wc4XZCnS5aD4hErY?p=preview

pparas
  • 549
  • 4
  • 13
  • Check out [this article](http://unixpapa.com/js/dyna.html). – trenthaynes May 20 '16 at 00:32
  • I did a lot of research and this is the first thing I found. I still don't understand from this article, how to access the data that might be loaded and parsed. – pparas May 20 '16 at 14:31
  • Just added a plunker. May be this will make it easier to try out what I am talking about. – pparas May 20 '16 at 14:44
  • *a plain use of eval()* — We've had `JSON.parse()` for a very long time now. Don't use `eval()`. – Quentin May 20 '16 at 14:50

0 Answers0