Is there a disadvantage when using JavaScript instead of an image when caching a web page?
I'm displaying a bar code on a web page and wondering if there is any disadvantage usi开发者_如何学编程ng JavaScript to do it rather than an image or pdf in terms of caching?
Caching will most likely be applied on scripts and images, so in that case there shouldn't be much difference. However, I think your image will change every now and then showing a different bar code? In that case a script that generated the code can have the advantage because the generation code never changes and will stay cached while the differing images must be downloaded again.
Javascript can be cached if it's in an external file, so go for it. Recognise however, that some browsers have Javascript disabled, it's probably more % of users than you would think with the popularity of plugins such as NoScript.
The advantage of an image is it will work on far far more browsers (100%). It can also be saved, if this is something visitors might do. Saving the output of a JS file is a little trickier for end users.
So I would favour an image, unless you are generating hundreds of new barcodes and have a solution in JS that makes that job a lot easier.
Cliffnotes: It depends on your situation. But hopefully I've outlined the most important pros/cons.
Any code you 'produce' via javascript cannot be cached
If you are using javascript to load an image or some other document then it will be cached.
精彩评论