Browser not taking updated values of javascript file - javascript

Recently i had faced a issue of javascript
my script include code
<script language="javascript" src="/scripts/validation.js"></script>
this is a common js file for all modules, as result this files contains some common funtions also,
every time when a common function is updated for new changes of our application , that changes doesnt reflect in the application
for example
validation.js
function validateDateValues(object)
{
some code//
}
function validateDate(val)
{
some code
}
On every update of our application, we have to change some codes in common function of js file,
if i do some change in code of validateDate(val), it doesnt relect in the application.
i think problem cause due to browser cache, when a client reports this issue , we recommend them to clear the cache and use it.
please give valuable suggestion to solve this issue

Usally a "force refresh" is enough. You can achieve this by pressing the following key combination:
Ctrl + F5
To know more refer this link
http://www.wikihow.com/Clear-the-Cache-in-Chrome

Whenever you do changes to your script file. Just add a ?(version without brackets) after your file source url.
Check the below link for detailed answer
How can I force clients to refresh JavaScript files?

This cases are mostly happens in IE browser,
For solution, you can add version parameter at the time of including JS file in application like as follow
<script src="/js/demo.js?v=1"></script>
Nex time if you make any change in js file then change that version from 1 to 2
<script src="/js/demo.js?v=2"></script>
I hope this will solve your problem for all browsers.

Related

I cant save my changes to my code in excel web add in (javascript)

I create an excel web add-in project using Visual studio 2019;
now I make some changes in parser.js a js file in the project,then press F5 to debug,when I step into the code in parser.js,I found it actually run the code in the "parser.js[dynamic]" which is the old version.
I've tried several times,restart,even restart my computer,no use.
What I did wrong???
thanks for JPO's answer,but I got something wrong.
today I changed my code, also my html code, html codes I change are as follows:
<script src="Scripts/Parser.js?version=1.3" type="text/javascript"></script>
<script src="Home.js?version=1.3" type="text/javascript"></script>
yes, the old version is ?version=1.2.
Then press F5 to debug and I get this:
the DOM Explorer Home.html and Home.html
as you can see,the Home.html is changed but the DOM Explorer isn't
it seems like the Home.html has been cached.I wonder if I did something wrong and what should I do?
if you need more information please tell me and I'll post
as mentioned in my comment, this looks like a typical caching problem. Browsers tend to cache resources which aren't supposed to change often. E.g. styles or scripts. Therefore for development, it might be necessary to have tools like "Clear Cache" installed in the browser to clear the caches quickly. Or if it is just one file you are working on, then appending a get parameter e.g. ?version=1 (and increasing the value every time you update code inside the parser.js should be enough. Just don't forget to remove the parameter when you publish your code as the page load times will suffer.
Welcome to our community!

Client-side loading of html file

I'm trying to write some html pages that I'm currently storing locally (on a Windows machine without any server running). I'd like to pull one file's html into another. I'm very novice when it comes to this kind of thing.
I found a couple of questions/answers on stackoverflow (1,2), but they both refer to deprecated jquery function "load". Specifically, it has a call to $("#myId").load("b.html");. Looking for how to migrate the load call to on, I see an answer that you need
$(window).on('load', function(){
// insert code here
});
Except I don't know what to put in the function itself. (The original answer just had a string that was the filename.
Any pointers?

Is it possible to prevent/remove any cached script declared by dojo.require?

I'm encountering an issue where most of a page's main logic is offset in a JS file and initialized within a dojo.require call to simplify debugging and development. We're encountering a case where an offload to another page, and then back to the first one and nothing inside our require script loads. I understand this is due in part to how dojo.require re-uses cached pages, but I can't go back to the cached version either. Is there a way, besides pasting all the scripts inside the page itself, to force Dojo to reload any require regardless of if it has been cached or not?
Force it to be reloaded in HTML.
<script type="text/javascript" src="path/to/my/module/name.js"></script>
<script type="text/javascript">
...
dojo.require("my.module.name");
...
</script>
I don't know if there is any problem on doing it but it does work.
Is there a reason you can't do this the "right" way, by putting the relevant code inside a function and then just calling said function again whenever you want?
If you still think you need to mess with the module loader though I think there are two alternatives after a quick check on the dojo.require source code:
Try to manually clear the module cache
delete d._loadedModules['my.module.name'];
Directly call the internal loader:
var relpath = d._getModuleSymbols(moduleName).join("/") + '.js';
d._loadPath(relpath, null);
(Try these at your own risk)

How do I stop js files being cached in IE?

I've created a page that uses the CKEditor javascript rich edit control.
It's a pretty neat control, especially seeing as it's free, but I'm having serious issues with the way it allows you to add templates.
To add a template you need to modify the templates js file in the CKEditor templates folder. The documentation page describing it is here.
This works fine until I want to update a template or add a new one (or anything else that requires me to modify the js file).
Internet Explorer caches the js file and doesn't pick up the update. Emptying the cache allows the update to be picked up, but this isn't an acceptable solution. Whenever I update a template I do not want to tell all of the users across the organisation to empty their IE cache. There must be a better way!
Is there a way to stop IE caching the js file? Or is there another solution to this problem?
Update
Ok, I found this section in the CKEditor API that will allow me to use the "insert timestamp into the url" solution suggested by several people.
So the script now looks like this:
config.templates_files =
[
'/editor_templates/site_default.js?time=' + utcTimeMilliseconds
];
Thanks for your help guys.
You can add rand seed to your js file. I mean <script src='jsFile.js?seed=12345'
And every time you want to empty cache - change seed number
Update:
as I understood you have to write like this config.templates_files = [ '/mytemplates.js?seed=12345' ];
Youo can add a timestamp query parameter when you include your .js file..
so instead of <script type="text/javascript" src="somefile.js"></script> you can <script type="text/javascript" src="somefile.js?timestampgoeshere"></script>
this should make the file to always get reloaded (the timestamp needs to be dynamic and changing for each load of the page..)
I am afraid you'll have to hack into the FCKEditor code and force the client JavaScript to load fresh copy of the XML file. You can do so by appending a ?random=<a random number> to the URL of the XML file being requested. FCKEditor is opensource so you should be able to locate the lines the request the XML and modify accordingly.
Set Expires-Header accordingly, e.g. in Apache
ExpiresActive On
ExpiresByType text/javascript access
This is not recommended for a real web application, only for intranet scenarios because the files will not be cachable.
every time you load the js file, pass a variable of a random number as a variable.
src='/libs/js/myfile.js?4859487594573
same trick for ajax loaded files.
Multiple methods (don't need to do them all):
press ^F5 (control + F5) - that'll load without cache
set pragma/cache headers on sending
use a random variable in the GET query string
.NET / C# :
public static void DisallowBrowserCache( )
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetAllowResponseInBrowserHistory(false);
}
You could make ASP.NET write a js file to the outputstream (http://server.com/jsFile.aspx, set http headers), and control the caching behavior of the response with the above method.

Re-executing JavaScript files

I asked this sort of question before ( Application fails to dynamically _re_load JavaScript files ) but I couldn't quite resolve the problem (if it has any solution), so I will put this in another fashion, a simpler one:
Can one unload a file from the browser's memory for posterior reloading?
(Removing the tag is not enough apparently.)
Or more relevant, if a reinsert the tag after removing it, is that code rerun (apparently not)?
How can accomplish the latter?
Thanks in advance.
You could generate a random number and then attach it to the end of the filename like this: .../script.js?r=0.25300762383267283. Then the browser would think it's a new file and not reference it from the cache.
I don't think it is possible to unload a script file.
As to the re-run issue, you could try giving each instance you call a JS file a varying GET parameter (e.g. the current timestamp). That might / should cause the browser to re-execute the file.
What are you trying to achieve? There may be smarter ways than re-loading a script file.

Categories