Solved

Remove Javascript file from client browsers

Posted on 2014-09-24
4
360 Views
Last Modified: 2014-09-27
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
Comment
Question by:yadavdep
4 Comments
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 167 total points
Comment Utility
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
 
LVL 30

Assisted Solution

by:Alexandre Simões
Alexandre Simões earned 167 total points
Comment Utility
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
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 166 total points
Comment Utility
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
 
LVL 15

Expert Comment

by:Insoftservice
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now