Solved

Remove Javascript file from client browsers

Posted on 2014-09-24
4
386 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 167 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 167 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 82

Assisted Solution

by:leakim971
leakim971 earned 166 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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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

632 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