0

I use ajax to post. And After the post, I append them to the current page successfully.

var content= $('#content').val();

$.ajax({
type: "POST",datatype:"json",  async: false, cache: true,
contentType: "application/x-www-form-urlencoded", 
url: "/share.php",        
data: "content=" + content,
success: function(html){
$(body).append();
}
});

However, on share.php, there are these lines

<script src="jquery.js"></script>
<script type="text/javascript" src="fancybox/source/jquery.fancybox.js?v=2.0.6"></script>

These are needed to make js gallery work on dynamically added contents added from share.php to the current page. It works successfully.

But the problem is, browsers always cache again for js files.

enter image description here

I can add all the script into that page using

<script>
// all js content
</script>

But that would be an unprofessional approach and it could take too much bandwith.

What is the correct way to make browsers cache the scripts ?

user198989
  • 4,574
  • 19
  • 66
  • 95
  • Your worst problem here is using `async: false`. Don't use it, it is deprecated and has no meaning anyway – A. Wolff Dec 11 '15 at 10:17
  • http://stackoverflow.com/questions/311062/caching-javascript-files – RomanPerekhrest Dec 11 '15 at 10:19
  • if you have jQuery then just remove it from there and load only one script. – Jai Dec 11 '15 at 10:31
  • For me I want to delete all the js files there, but the dynamically appended items are images and when you click them they need these js files (for slide show) – user198989 Dec 11 '15 at 10:33
  • "Unprofessional approach" is a bit of a misunderstanding of your problem. If bandwidth is an issue, you can use bundling, minification and even CDNs. Also, you're already using jquery in your mainpage, so you can remove that from share.php if that page is not supposed to be reached directly. – bug-a-lot Dec 11 '15 at 10:38

0 Answers0