d-_-b
d-_-b

Reputation: 23191

Using javascript to get the size of a library on the page

Is it possible to get the size of a javascript library loaded on the site (<script src='..'></script>), using JavaScript?

For example, stackoverflow has a file //cdn.sstatic.net/Js/stub.en.js , is it possible to select that script tag using jQuery, and then somehow get its file size?

Thanks!

Upvotes: 0

Views: 389

Answers (1)

Aminadav Glickshtein
Aminadav Glickshtein

Reputation: 24600

No. You cannot access the data of any domain except your domain.

If it is in the same domain you can do something like that:

$.ajax({url:'//cdn.sstatic.net/Js/stub.en.js',success:function (a){ alert(a.length) } } )

Read more about CORS: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

You can serve the file from your server Then you can do:

$.ajax({url:'stub.en.js',success:function (a){ alert(a.length) } } )

You just need to develop a small server-side script to load it from the CDN (aka PROXY)

Upvotes: 1

Related Questions