Solved

Remove Javascript file from client browsers

Posted on 2014-09-24
4
365 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn how to dynamically set the form action using jQuery.
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)

896 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

15 Experts available now in Live!

Get 1:1 Help Now