Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Remove Javascript file from client browsers

Posted on 2014-09-24
4
Medium Priority
?
417 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 36

Accepted Solution

by:
Miguel Oz earned 668 total points
ID: 40340982
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 668 total points
ID: 40340993
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 83

Assisted Solution

by:leakim971
leakim971 earned 664 total points
ID: 40341054
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
ID: 40341191
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

885 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