Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

Remove Javascript file from client browsers

In my web application, I have used external Javascript files.
Now my question is because these js files will be downloaded on client machine when he access the web form,
If I make change in any of such JS file and wanted to make sure that client have the latest file to avoid any issue because of older JS file.
Is there any way of doing it except telling the clients to clear his browser history?
0
yadavdep
Asked:
yadavdep
3 Solutions
 
Miguel OzSoftware EngineerCommented:
Yes, there is a solution that adds a version information to your JS filename to make it unique as shown below:
 <script type='text/javascript' src='/MyJavascriptFile.js?v=123'></script>

Open in new window


where v=123 contains the version. For further details,  check here.
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
No, you can't.
What you can do is version or timestamp your script files.

For instance, to always force script loading you can do:
<script src="/scripts/myscript.js?_=283748"></script>

Open in new window

Where _=283748 is a timestamp generated server-side on page load. In this case myscript.js file will always have the same name but as the timestamp will always make the URL unique, the browser won't cache it.

Tho this works, this technique is far from optimal. This will prevent the browser from caching your static resources which isn't what you want.

So what you can do is change the script file name each time you release a new version of it:
<script src="/scripts/myscript.v1.js"></script>

Open in new window

On the next release to production this can become:
<script src="/scripts/myscript.v1.1.js"></script>

Open in new window

This can be done manually but it's tedious. Automating this is far more useful and desirable and can actually be done to all your resources, independently if they were changed or not.
Of course this requires that you already have your packaging process automated (as you should!)

Got this I would recommend having a look at Grunt and grunt-usemin

Cheers!
0
 
leakim971PluritechnicianCommented:
you may prevent caching using a loader :

<script src="/path/to/jquery.js"></script>
<script>
     jQuery(function($) {
            $.getScript("/path/to/business.js?d=" + new Date().getTime()); // this will prevent caching of the file business.js
     });
</script>

Open in new window

http://api.jquery.com/jquery.getscript/
0
 
InsoftserviceCommented:
JS ,css are stored in browser cache so that it can be accessed from it directly so as to avoid fetching the data from server.
It gets clear when there is any minute change in your URL or path of js and css
So the main concept of clearing cache is to change your url as compared to previous one.
That's the reason why you would see when you fetch jquery it has version or any site they have version .
Time stamp would resolve your issue 100% but would fail your cache mechanism .
Which is much more worse than providing old file as unnecessary we would be increasing hit on our server and on client side.

<script src="http://lovetomarry.com/scripts/testscript.v1.js"></script>
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now