admin管理员组文章数量:1417414
I am developing a web application. I am using a main javascript file that is used in most of the forms.
<script src="assets/public.js"></script>
This file is being updated regularly and the users can't get the last updated file because the old file is cached with browser. So I am using a version indicator at the end of the file name to enforce browsers to use the latest version of my javascript file :
<script src="assets/public.js?version=54"></script>
I should update the version in every form after each change in the javascript file. this is a manual task which is really time-consuming and some forms might be missed to update.
Is there any effective way to define the last version as a constant and automatically update all forms regarding the last version?
I am developing a web application. I am using a main javascript file that is used in most of the forms.
<script src="assets/public.js"></script>
This file is being updated regularly and the users can't get the last updated file because the old file is cached with browser. So I am using a version indicator at the end of the file name to enforce browsers to use the latest version of my javascript file :
<script src="assets/public.js?version=54"></script>
I should update the version in every form after each change in the javascript file. this is a manual task which is really time-consuming and some forms might be missed to update.
Is there any effective way to define the last version as a constant and automatically update all forms regarding the last version?
Share Improve this question asked Aug 6, 2019 at 7:13 BehnamBehnam 1,0633 gold badges14 silver badges39 bronze badges 1- how are you developing the app? If you are including the mon scripts on every page and not using templates then that itself is bad practice? – Tarun Lalwani Commented Aug 13, 2019 at 6:03
2 Answers
Reset to default 2In your project you need to have a folder [App_Start] there in the [BundleConfig.cs] file you can register bundles:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/public-{version}.js"));
and in the view add this:
<head>
@Scripts.Render("~/bundles/public")
</head>
Every time when change .js version update variable {version} from the file [BundleConfig.cs]
It's possible to use the last modified date of the file as the version indicator. So even no need to define constant.
<script src="asset/public.js?version={{lastModifiedDate('asset/public.js')}}"></script>
Below function returns the last modified date of file:
function lastModifiedDate(url) {
try {
var req = new XMLHttpRequest();
req.open("HEAD", url, false);
req.send(null);
if (req.status == 200) {
return req.getResponseHeader('Last-Modified');
}
else return false;
} catch (er) {
return er.message;
}
}
本文标签: How to update version of a javascript file in all forms in visual studioStack Overflow
版权声明:本文标题:How to update version of a javascript file in all forms in visual studio - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745265517a2650574.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论