Append Dynamic version(variable ) in Script tag and stylesheet based on time
<script src="/assets/abc.js?v='+new Date.getTime();" type="text/javascript"></script>
<link href="/assets/cder.css?v='+new Date.getTime();" rel="stylesheet"></link>
or,
var myVariable = Math.floor(Math.random() * 999999999999);
<script src="/assets/abc.js?v='+myVariable ;" type="text/javascript"></script>
<link href="/assets/cder.css?v='+new Date.getTime();" rel="stylesheet"></link>
I have tried this as below but the script is not loading on network tab.
<script type="text/javascript>
var script = document.createElement('script');
script.setAttribute('src', '/assets/abc.js?v=' + new Date.getTime());
var head1 = document.getElementsByTagName("head")[0];
head1.appendChild(script);
</script>
I am trying to add dynamic version(variable ) in script tag and stylesheet based on current time or some dynamic variable?
If possible, Please help me with the shortest and efficient solution.
How to achieve that?
javascript angular angular7
add a comment |
<script src="/assets/abc.js?v='+new Date.getTime();" type="text/javascript"></script>
<link href="/assets/cder.css?v='+new Date.getTime();" rel="stylesheet"></link>
or,
var myVariable = Math.floor(Math.random() * 999999999999);
<script src="/assets/abc.js?v='+myVariable ;" type="text/javascript"></script>
<link href="/assets/cder.css?v='+new Date.getTime();" rel="stylesheet"></link>
I have tried this as below but the script is not loading on network tab.
<script type="text/javascript>
var script = document.createElement('script');
script.setAttribute('src', '/assets/abc.js?v=' + new Date.getTime());
var head1 = document.getElementsByTagName("head")[0];
head1.appendChild(script);
</script>
I am trying to add dynamic version(variable ) in script tag and stylesheet based on current time or some dynamic variable?
If possible, Please help me with the shortest and efficient solution.
How to achieve that?
javascript angular angular7
1
Is date and time necessary? I have a similar solution but using version number as apposed to date and time
– Luke Walker
Nov 27 at 11:42
Can you please show me it the variable should be dynamic and should not match with the previous variable value once script or stylesheet is loaded.
– Mahi
Nov 27 at 11:47
Ah i see. My version works well for production use for every time i push code. Not on areload
basis.
– Luke Walker
Nov 27 at 11:48
do you need this for your production version to prevent caching in new versions of your website or just for development? if you specify for what purpose you want this I can help you.
– molikh
Nov 30 at 8:52
@molikh, I am wondering, the above tag resides in index.html and does it matter if this index.html is for production or development. If you can help me in either, this may solve my problem
– Mahi
Nov 30 at 9:19
add a comment |
<script src="/assets/abc.js?v='+new Date.getTime();" type="text/javascript"></script>
<link href="/assets/cder.css?v='+new Date.getTime();" rel="stylesheet"></link>
or,
var myVariable = Math.floor(Math.random() * 999999999999);
<script src="/assets/abc.js?v='+myVariable ;" type="text/javascript"></script>
<link href="/assets/cder.css?v='+new Date.getTime();" rel="stylesheet"></link>
I have tried this as below but the script is not loading on network tab.
<script type="text/javascript>
var script = document.createElement('script');
script.setAttribute('src', '/assets/abc.js?v=' + new Date.getTime());
var head1 = document.getElementsByTagName("head")[0];
head1.appendChild(script);
</script>
I am trying to add dynamic version(variable ) in script tag and stylesheet based on current time or some dynamic variable?
If possible, Please help me with the shortest and efficient solution.
How to achieve that?
javascript angular angular7
<script src="/assets/abc.js?v='+new Date.getTime();" type="text/javascript"></script>
<link href="/assets/cder.css?v='+new Date.getTime();" rel="stylesheet"></link>
or,
var myVariable = Math.floor(Math.random() * 999999999999);
<script src="/assets/abc.js?v='+myVariable ;" type="text/javascript"></script>
<link href="/assets/cder.css?v='+new Date.getTime();" rel="stylesheet"></link>
I have tried this as below but the script is not loading on network tab.
<script type="text/javascript>
var script = document.createElement('script');
script.setAttribute('src', '/assets/abc.js?v=' + new Date.getTime());
var head1 = document.getElementsByTagName("head")[0];
head1.appendChild(script);
</script>
I am trying to add dynamic version(variable ) in script tag and stylesheet based on current time or some dynamic variable?
If possible, Please help me with the shortest and efficient solution.
How to achieve that?
javascript angular angular7
javascript angular angular7
edited Nov 28 at 19:38
asked Nov 22 at 18:57
Mahi
690319
690319
1
Is date and time necessary? I have a similar solution but using version number as apposed to date and time
– Luke Walker
Nov 27 at 11:42
Can you please show me it the variable should be dynamic and should not match with the previous variable value once script or stylesheet is loaded.
– Mahi
Nov 27 at 11:47
Ah i see. My version works well for production use for every time i push code. Not on areload
basis.
– Luke Walker
Nov 27 at 11:48
do you need this for your production version to prevent caching in new versions of your website or just for development? if you specify for what purpose you want this I can help you.
– molikh
Nov 30 at 8:52
@molikh, I am wondering, the above tag resides in index.html and does it matter if this index.html is for production or development. If you can help me in either, this may solve my problem
– Mahi
Nov 30 at 9:19
add a comment |
1
Is date and time necessary? I have a similar solution but using version number as apposed to date and time
– Luke Walker
Nov 27 at 11:42
Can you please show me it the variable should be dynamic and should not match with the previous variable value once script or stylesheet is loaded.
– Mahi
Nov 27 at 11:47
Ah i see. My version works well for production use for every time i push code. Not on areload
basis.
– Luke Walker
Nov 27 at 11:48
do you need this for your production version to prevent caching in new versions of your website or just for development? if you specify for what purpose you want this I can help you.
– molikh
Nov 30 at 8:52
@molikh, I am wondering, the above tag resides in index.html and does it matter if this index.html is for production or development. If you can help me in either, this may solve my problem
– Mahi
Nov 30 at 9:19
1
1
Is date and time necessary? I have a similar solution but using version number as apposed to date and time
– Luke Walker
Nov 27 at 11:42
Is date and time necessary? I have a similar solution but using version number as apposed to date and time
– Luke Walker
Nov 27 at 11:42
Can you please show me it the variable should be dynamic and should not match with the previous variable value once script or stylesheet is loaded.
– Mahi
Nov 27 at 11:47
Can you please show me it the variable should be dynamic and should not match with the previous variable value once script or stylesheet is loaded.
– Mahi
Nov 27 at 11:47
Ah i see. My version works well for production use for every time i push code. Not on a
reload
basis.– Luke Walker
Nov 27 at 11:48
Ah i see. My version works well for production use for every time i push code. Not on a
reload
basis.– Luke Walker
Nov 27 at 11:48
do you need this for your production version to prevent caching in new versions of your website or just for development? if you specify for what purpose you want this I can help you.
– molikh
Nov 30 at 8:52
do you need this for your production version to prevent caching in new versions of your website or just for development? if you specify for what purpose you want this I can help you.
– molikh
Nov 30 at 8:52
@molikh, I am wondering, the above tag resides in index.html and does it matter if this index.html is for production or development. If you can help me in either, this may solve my problem
– Mahi
Nov 30 at 9:19
@molikh, I am wondering, the above tag resides in index.html and does it matter if this index.html is for production or development. If you can help me in either, this may solve my problem
– Mahi
Nov 30 at 9:19
add a comment |
8 Answers
8
active
oldest
votes
If you are looking for the shortest solution, how about this?
<script>document.write('<link href="/assets/cder.css?v=' + Date.now() + '" rel="stylesheet" />');</script>
A worthy alternative should be:
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = '/assets/cder.css?v=' + Date.now();
document.body.appendChild(link);
</script>
Well, you must escape the closing script tag as follows:
<script>document.write('<script src="/assets/abc.js?v=' + Date.now() + '"></script>');</script>
An example of how to add several scripts:
<script>
var scripts = [
'/assets/abc.js',
'/assets/def.js',
];
for (var i = 0; i < scripts.length; i++) {
var script = document.createElement('script');
script.onerror = function() {
alert('Could not load ' + this.src);
};
script.src = scripts[i] + '?v=' + Date.now();
document.body.appendChild(script);
}
</script>
Thanks, Can you please some similar solution for script as well. I have 7 script loading in head tag and the two scripts I need similar as stylesheet.
– Mahi
Nov 30 at 19:24
1
Please check my update. By the way, it seems you need to use the search before asking such basic questions.
– Victor
Nov 30 at 19:37
<script> var link = document.createElement('script'); link.type= 'text/javascript'; link.src= '/assets/cder.js?v=' + Date.now(); document.body.appendChild(link); </script> <script> var link1 = document.createElement('script'); link1.type= 'text/javascript'; link1.src= '/assets/abc.js?v=' + Date.now(); document.body.appendChild(link1); </script>
– Mahi
Nov 30 at 19:56
Maybe, this is a basic question.But, I am not aware of it. This above solution in comment of mine is not working, if I am trying for my two scripts. Can you please suggest what I am doing wrong.
– Mahi
Nov 30 at 20:03
Check my update.
– Victor
Nov 30 at 20:23
|
show 4 more comments
You could do this dynamically from javascript.
<script>
var my_awesome_script = document.createElement('script');
my_awesome_script.setAttribute('src', '/assets/abc.js?v='+new Date.getTime()); //was missing single quote
document.head.appendChild(my_awesome_script);
</script>
taken from
Stack overflow answer
I have tried this but script is not being loaded on network tab.
– Mahi
Nov 24 at 19:21
what about this? stackoverflow.com/questions/1866717/…
– Vaggelis Ksps
Nov 24 at 19:22
I think that this will help you but the best approach for me is to do all these from backend if it is possible. Javascript runs in the browser so the user has to wait for all these calculations and requests to finish to get the proper view of the dom. I am not sure if the user experience would be the same if a script or a css file come after the page load. stackoverflow.com/questions/20821720/…
– Vaggelis Ksps
Nov 24 at 19:32
add a comment |
<script [src]="'/assets/abc.js?v='+new Date.getTime();" type="text/javascript"></script>
if its not working try to create model and do it like this:
this.someModel = "/assets/abc.js?v='"+new Date.getTime();
<script [src]="script" type="text/javascript"></script>
there are also more ways to do it.
Its not working, The scripts are not showing on network tab.
– Mahi
Nov 22 at 20:29
Kinda wierd, have you tried to put ur script on header and see if its actully loading? becouse that wierd what your saying.
– Talg123
Nov 26 at 18:25
add a comment |
From all the testing I have done it seems that the final attempt that you tried works flawlessly when using [0] instead of [6] or any other value for that matter. Also why do you have multiple head tags? was this a typo and you meant header? it seems to work with any number of headers.
But really this type of thing should be handled via backend if possible.
Sorry, it was a typo. Updated question. It does not help
– Mahi
Nov 30 at 4:42
add a comment |
It seems that you are confused with html
& javascript
.
It's impossible to use html
which is mixed up with javascript
to archive something.
With SSR(Server Side Render)
Using such as Handlebars
or another template engine.
The timestamp
(as well as your version tag) should be rendered in server side before html was generated.
<script src="/assets/abc.js?v='+new Date.getTime();" type="text/javascript"></script>
<link href="/assets/cder.css?v='+new Date.getTime();" rel="stylesheet"></link>
Without SSR(Server Side Render)
We can archive with javascript
after html
is returned by HTTP
request.
let script = document.createElement('script');
script.setAttribute('src', `somefile?v=${Date.now()}`);
document.head.appendChild(script);
It's better to wrap a function to do this job.
add a comment |
Your script is not loading because its not new Date.getTime()
but new Date().getTime()
or just use Date.now()
. Additionally To prevent browser cache you can add no-cache header for example using .htaccess
<filesMatch ".(js|css)">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</filesMatch>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/javascript "access plus 1 seconds"
ExpiresByType text/css "access plus 1 seconds"
</IfModule>
and for javascript function
function appendJS(url) {
var s = document.createElement('script');
s.src = url + '?v=' + Date.now();
document.body.appendChild(s);
}
function appendCSS(url) {
var s = document.createElement('link');
s.rel = 'stylesheet';
s.href = url + '?v=' + Date.now();
document.body.appendChild(s);
}
appendJS('/assets/abc.js');
appendCSS('/assets/cder.css');
add a comment |
While other answers here correctly describe how to do this in a script, I want to note three things:
- You should do this at build-time using hashes, not timestamps, otherwise, every time the date changes your cache could be cleared. That's not a great user experience. Be intentional about when and why you're fetching this. Webpack has a nifty build plugin for creating hashes in file names.
- Use headers to drive whether a new file should be fetched.
- Make sure you're setting the
async
attribute on any run-time<script>
element, otherwise it degrades performance, sometimes significantly. Search engines penalize this heavily.
```
```
But note that appending a script tag regardless of using the async
attribute will block rendering. Read this for more details.
Hi, please give a example for better understanding
– Mahi
Dec 1 at 5:16
add a comment |
const time = new Date(2018, 10, 24, 22); // you can provide minutes and seconds if you want
const today = new Date();
const head = document.querySelector('html head');
const body = document.querySelector('html body');
const styleFile = '<link href="/assets/cder.css?v=' + today.getTime() + '" rel="stylesheet"></link>';
const scriptFile = '<script src="/assets/abc.js?v=' + today.getTime() + '" type="text/javascript"></script>';
setInterval(() => {
if ((time.getDate() === today.getDate()) && (time.getMonth() === today.getMonth()) && (time.getFullYear() === today.getFullYear()) && (time.getHours() === today.getHours())) {
head.append(styleFile);
body.append(scriptFile);
}
}, 60000);
<html>
<head>
<!-- here we will append style file -->
</head>
<body>
<div>some dummy text</div>
<!-- here we will append script file -->
</body>
</html>
why setInterval
? that how we tell the browser "please check every minute if the condition true" if it's, so append
new styleFile and scriptFile .
Note: you can provide minutes
and seconds
in time as well if you want but remember to also add the appropriate condition in if(...){}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436768%2fappend-dynamic-versionvariable-in-script-tag-and-stylesheet-based-on-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you are looking for the shortest solution, how about this?
<script>document.write('<link href="/assets/cder.css?v=' + Date.now() + '" rel="stylesheet" />');</script>
A worthy alternative should be:
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = '/assets/cder.css?v=' + Date.now();
document.body.appendChild(link);
</script>
Well, you must escape the closing script tag as follows:
<script>document.write('<script src="/assets/abc.js?v=' + Date.now() + '"></script>');</script>
An example of how to add several scripts:
<script>
var scripts = [
'/assets/abc.js',
'/assets/def.js',
];
for (var i = 0; i < scripts.length; i++) {
var script = document.createElement('script');
script.onerror = function() {
alert('Could not load ' + this.src);
};
script.src = scripts[i] + '?v=' + Date.now();
document.body.appendChild(script);
}
</script>
Thanks, Can you please some similar solution for script as well. I have 7 script loading in head tag and the two scripts I need similar as stylesheet.
– Mahi
Nov 30 at 19:24
1
Please check my update. By the way, it seems you need to use the search before asking such basic questions.
– Victor
Nov 30 at 19:37
<script> var link = document.createElement('script'); link.type= 'text/javascript'; link.src= '/assets/cder.js?v=' + Date.now(); document.body.appendChild(link); </script> <script> var link1 = document.createElement('script'); link1.type= 'text/javascript'; link1.src= '/assets/abc.js?v=' + Date.now(); document.body.appendChild(link1); </script>
– Mahi
Nov 30 at 19:56
Maybe, this is a basic question.But, I am not aware of it. This above solution in comment of mine is not working, if I am trying for my two scripts. Can you please suggest what I am doing wrong.
– Mahi
Nov 30 at 20:03
Check my update.
– Victor
Nov 30 at 20:23
|
show 4 more comments
If you are looking for the shortest solution, how about this?
<script>document.write('<link href="/assets/cder.css?v=' + Date.now() + '" rel="stylesheet" />');</script>
A worthy alternative should be:
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = '/assets/cder.css?v=' + Date.now();
document.body.appendChild(link);
</script>
Well, you must escape the closing script tag as follows:
<script>document.write('<script src="/assets/abc.js?v=' + Date.now() + '"></script>');</script>
An example of how to add several scripts:
<script>
var scripts = [
'/assets/abc.js',
'/assets/def.js',
];
for (var i = 0; i < scripts.length; i++) {
var script = document.createElement('script');
script.onerror = function() {
alert('Could not load ' + this.src);
};
script.src = scripts[i] + '?v=' + Date.now();
document.body.appendChild(script);
}
</script>
Thanks, Can you please some similar solution for script as well. I have 7 script loading in head tag and the two scripts I need similar as stylesheet.
– Mahi
Nov 30 at 19:24
1
Please check my update. By the way, it seems you need to use the search before asking such basic questions.
– Victor
Nov 30 at 19:37
<script> var link = document.createElement('script'); link.type= 'text/javascript'; link.src= '/assets/cder.js?v=' + Date.now(); document.body.appendChild(link); </script> <script> var link1 = document.createElement('script'); link1.type= 'text/javascript'; link1.src= '/assets/abc.js?v=' + Date.now(); document.body.appendChild(link1); </script>
– Mahi
Nov 30 at 19:56
Maybe, this is a basic question.But, I am not aware of it. This above solution in comment of mine is not working, if I am trying for my two scripts. Can you please suggest what I am doing wrong.
– Mahi
Nov 30 at 20:03
Check my update.
– Victor
Nov 30 at 20:23
|
show 4 more comments
If you are looking for the shortest solution, how about this?
<script>document.write('<link href="/assets/cder.css?v=' + Date.now() + '" rel="stylesheet" />');</script>
A worthy alternative should be:
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = '/assets/cder.css?v=' + Date.now();
document.body.appendChild(link);
</script>
Well, you must escape the closing script tag as follows:
<script>document.write('<script src="/assets/abc.js?v=' + Date.now() + '"></script>');</script>
An example of how to add several scripts:
<script>
var scripts = [
'/assets/abc.js',
'/assets/def.js',
];
for (var i = 0; i < scripts.length; i++) {
var script = document.createElement('script');
script.onerror = function() {
alert('Could not load ' + this.src);
};
script.src = scripts[i] + '?v=' + Date.now();
document.body.appendChild(script);
}
</script>
If you are looking for the shortest solution, how about this?
<script>document.write('<link href="/assets/cder.css?v=' + Date.now() + '" rel="stylesheet" />');</script>
A worthy alternative should be:
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = '/assets/cder.css?v=' + Date.now();
document.body.appendChild(link);
</script>
Well, you must escape the closing script tag as follows:
<script>document.write('<script src="/assets/abc.js?v=' + Date.now() + '"></script>');</script>
An example of how to add several scripts:
<script>
var scripts = [
'/assets/abc.js',
'/assets/def.js',
];
for (var i = 0; i < scripts.length; i++) {
var script = document.createElement('script');
script.onerror = function() {
alert('Could not load ' + this.src);
};
script.src = scripts[i] + '?v=' + Date.now();
document.body.appendChild(script);
}
</script>
<script>
var scripts = [
'/assets/abc.js',
'/assets/def.js',
];
for (var i = 0; i < scripts.length; i++) {
var script = document.createElement('script');
script.onerror = function() {
alert('Could not load ' + this.src);
};
script.src = scripts[i] + '?v=' + Date.now();
document.body.appendChild(script);
}
</script>
<script>
var scripts = [
'/assets/abc.js',
'/assets/def.js',
];
for (var i = 0; i < scripts.length; i++) {
var script = document.createElement('script');
script.onerror = function() {
alert('Could not load ' + this.src);
};
script.src = scripts[i] + '?v=' + Date.now();
document.body.appendChild(script);
}
</script>
edited Dec 1 at 6:39
answered Nov 30 at 16:03
Victor
3,46811722
3,46811722
Thanks, Can you please some similar solution for script as well. I have 7 script loading in head tag and the two scripts I need similar as stylesheet.
– Mahi
Nov 30 at 19:24
1
Please check my update. By the way, it seems you need to use the search before asking such basic questions.
– Victor
Nov 30 at 19:37
<script> var link = document.createElement('script'); link.type= 'text/javascript'; link.src= '/assets/cder.js?v=' + Date.now(); document.body.appendChild(link); </script> <script> var link1 = document.createElement('script'); link1.type= 'text/javascript'; link1.src= '/assets/abc.js?v=' + Date.now(); document.body.appendChild(link1); </script>
– Mahi
Nov 30 at 19:56
Maybe, this is a basic question.But, I am not aware of it. This above solution in comment of mine is not working, if I am trying for my two scripts. Can you please suggest what I am doing wrong.
– Mahi
Nov 30 at 20:03
Check my update.
– Victor
Nov 30 at 20:23
|
show 4 more comments
Thanks, Can you please some similar solution for script as well. I have 7 script loading in head tag and the two scripts I need similar as stylesheet.
– Mahi
Nov 30 at 19:24
1
Please check my update. By the way, it seems you need to use the search before asking such basic questions.
– Victor
Nov 30 at 19:37
<script> var link = document.createElement('script'); link.type= 'text/javascript'; link.src= '/assets/cder.js?v=' + Date.now(); document.body.appendChild(link); </script> <script> var link1 = document.createElement('script'); link1.type= 'text/javascript'; link1.src= '/assets/abc.js?v=' + Date.now(); document.body.appendChild(link1); </script>
– Mahi
Nov 30 at 19:56
Maybe, this is a basic question.But, I am not aware of it. This above solution in comment of mine is not working, if I am trying for my two scripts. Can you please suggest what I am doing wrong.
– Mahi
Nov 30 at 20:03
Check my update.
– Victor
Nov 30 at 20:23
Thanks, Can you please some similar solution for script as well. I have 7 script loading in head tag and the two scripts I need similar as stylesheet.
– Mahi
Nov 30 at 19:24
Thanks, Can you please some similar solution for script as well. I have 7 script loading in head tag and the two scripts I need similar as stylesheet.
– Mahi
Nov 30 at 19:24
1
1
Please check my update. By the way, it seems you need to use the search before asking such basic questions.
– Victor
Nov 30 at 19:37
Please check my update. By the way, it seems you need to use the search before asking such basic questions.
– Victor
Nov 30 at 19:37
<script> var link = document.createElement('script'); link.type= 'text/javascript'; link.src= '/assets/cder.js?v=' + Date.now(); document.body.appendChild(link); </script> <script> var link1 = document.createElement('script'); link1.type= 'text/javascript'; link1.src= '/assets/abc.js?v=' + Date.now(); document.body.appendChild(link1); </script>
– Mahi
Nov 30 at 19:56
<script> var link = document.createElement('script'); link.type= 'text/javascript'; link.src= '/assets/cder.js?v=' + Date.now(); document.body.appendChild(link); </script> <script> var link1 = document.createElement('script'); link1.type= 'text/javascript'; link1.src= '/assets/abc.js?v=' + Date.now(); document.body.appendChild(link1); </script>
– Mahi
Nov 30 at 19:56
Maybe, this is a basic question.But, I am not aware of it. This above solution in comment of mine is not working, if I am trying for my two scripts. Can you please suggest what I am doing wrong.
– Mahi
Nov 30 at 20:03
Maybe, this is a basic question.But, I am not aware of it. This above solution in comment of mine is not working, if I am trying for my two scripts. Can you please suggest what I am doing wrong.
– Mahi
Nov 30 at 20:03
Check my update.
– Victor
Nov 30 at 20:23
Check my update.
– Victor
Nov 30 at 20:23
|
show 4 more comments
You could do this dynamically from javascript.
<script>
var my_awesome_script = document.createElement('script');
my_awesome_script.setAttribute('src', '/assets/abc.js?v='+new Date.getTime()); //was missing single quote
document.head.appendChild(my_awesome_script);
</script>
taken from
Stack overflow answer
I have tried this but script is not being loaded on network tab.
– Mahi
Nov 24 at 19:21
what about this? stackoverflow.com/questions/1866717/…
– Vaggelis Ksps
Nov 24 at 19:22
I think that this will help you but the best approach for me is to do all these from backend if it is possible. Javascript runs in the browser so the user has to wait for all these calculations and requests to finish to get the proper view of the dom. I am not sure if the user experience would be the same if a script or a css file come after the page load. stackoverflow.com/questions/20821720/…
– Vaggelis Ksps
Nov 24 at 19:32
add a comment |
You could do this dynamically from javascript.
<script>
var my_awesome_script = document.createElement('script');
my_awesome_script.setAttribute('src', '/assets/abc.js?v='+new Date.getTime()); //was missing single quote
document.head.appendChild(my_awesome_script);
</script>
taken from
Stack overflow answer
I have tried this but script is not being loaded on network tab.
– Mahi
Nov 24 at 19:21
what about this? stackoverflow.com/questions/1866717/…
– Vaggelis Ksps
Nov 24 at 19:22
I think that this will help you but the best approach for me is to do all these from backend if it is possible. Javascript runs in the browser so the user has to wait for all these calculations and requests to finish to get the proper view of the dom. I am not sure if the user experience would be the same if a script or a css file come after the page load. stackoverflow.com/questions/20821720/…
– Vaggelis Ksps
Nov 24 at 19:32
add a comment |
You could do this dynamically from javascript.
<script>
var my_awesome_script = document.createElement('script');
my_awesome_script.setAttribute('src', '/assets/abc.js?v='+new Date.getTime()); //was missing single quote
document.head.appendChild(my_awesome_script);
</script>
taken from
Stack overflow answer
You could do this dynamically from javascript.
<script>
var my_awesome_script = document.createElement('script');
my_awesome_script.setAttribute('src', '/assets/abc.js?v='+new Date.getTime()); //was missing single quote
document.head.appendChild(my_awesome_script);
</script>
taken from
Stack overflow answer
edited Dec 2 at 3:46
Mark Eriksson
1,153716
1,153716
answered Nov 24 at 19:19
Vaggelis Ksps
1939
1939
I have tried this but script is not being loaded on network tab.
– Mahi
Nov 24 at 19:21
what about this? stackoverflow.com/questions/1866717/…
– Vaggelis Ksps
Nov 24 at 19:22
I think that this will help you but the best approach for me is to do all these from backend if it is possible. Javascript runs in the browser so the user has to wait for all these calculations and requests to finish to get the proper view of the dom. I am not sure if the user experience would be the same if a script or a css file come after the page load. stackoverflow.com/questions/20821720/…
– Vaggelis Ksps
Nov 24 at 19:32
add a comment |
I have tried this but script is not being loaded on network tab.
– Mahi
Nov 24 at 19:21
what about this? stackoverflow.com/questions/1866717/…
– Vaggelis Ksps
Nov 24 at 19:22
I think that this will help you but the best approach for me is to do all these from backend if it is possible. Javascript runs in the browser so the user has to wait for all these calculations and requests to finish to get the proper view of the dom. I am not sure if the user experience would be the same if a script or a css file come after the page load. stackoverflow.com/questions/20821720/…
– Vaggelis Ksps
Nov 24 at 19:32
I have tried this but script is not being loaded on network tab.
– Mahi
Nov 24 at 19:21
I have tried this but script is not being loaded on network tab.
– Mahi
Nov 24 at 19:21
what about this? stackoverflow.com/questions/1866717/…
– Vaggelis Ksps
Nov 24 at 19:22
what about this? stackoverflow.com/questions/1866717/…
– Vaggelis Ksps
Nov 24 at 19:22
I think that this will help you but the best approach for me is to do all these from backend if it is possible. Javascript runs in the browser so the user has to wait for all these calculations and requests to finish to get the proper view of the dom. I am not sure if the user experience would be the same if a script or a css file come after the page load. stackoverflow.com/questions/20821720/…
– Vaggelis Ksps
Nov 24 at 19:32
I think that this will help you but the best approach for me is to do all these from backend if it is possible. Javascript runs in the browser so the user has to wait for all these calculations and requests to finish to get the proper view of the dom. I am not sure if the user experience would be the same if a script or a css file come after the page load. stackoverflow.com/questions/20821720/…
– Vaggelis Ksps
Nov 24 at 19:32
add a comment |
<script [src]="'/assets/abc.js?v='+new Date.getTime();" type="text/javascript"></script>
if its not working try to create model and do it like this:
this.someModel = "/assets/abc.js?v='"+new Date.getTime();
<script [src]="script" type="text/javascript"></script>
there are also more ways to do it.
Its not working, The scripts are not showing on network tab.
– Mahi
Nov 22 at 20:29
Kinda wierd, have you tried to put ur script on header and see if its actully loading? becouse that wierd what your saying.
– Talg123
Nov 26 at 18:25
add a comment |
<script [src]="'/assets/abc.js?v='+new Date.getTime();" type="text/javascript"></script>
if its not working try to create model and do it like this:
this.someModel = "/assets/abc.js?v='"+new Date.getTime();
<script [src]="script" type="text/javascript"></script>
there are also more ways to do it.
Its not working, The scripts are not showing on network tab.
– Mahi
Nov 22 at 20:29
Kinda wierd, have you tried to put ur script on header and see if its actully loading? becouse that wierd what your saying.
– Talg123
Nov 26 at 18:25
add a comment |
<script [src]="'/assets/abc.js?v='+new Date.getTime();" type="text/javascript"></script>
if its not working try to create model and do it like this:
this.someModel = "/assets/abc.js?v='"+new Date.getTime();
<script [src]="script" type="text/javascript"></script>
there are also more ways to do it.
<script [src]="'/assets/abc.js?v='+new Date.getTime();" type="text/javascript"></script>
if its not working try to create model and do it like this:
this.someModel = "/assets/abc.js?v='"+new Date.getTime();
<script [src]="script" type="text/javascript"></script>
there are also more ways to do it.
answered Nov 22 at 19:16
Talg123
21118
21118
Its not working, The scripts are not showing on network tab.
– Mahi
Nov 22 at 20:29
Kinda wierd, have you tried to put ur script on header and see if its actully loading? becouse that wierd what your saying.
– Talg123
Nov 26 at 18:25
add a comment |
Its not working, The scripts are not showing on network tab.
– Mahi
Nov 22 at 20:29
Kinda wierd, have you tried to put ur script on header and see if its actully loading? becouse that wierd what your saying.
– Talg123
Nov 26 at 18:25
Its not working, The scripts are not showing on network tab.
– Mahi
Nov 22 at 20:29
Its not working, The scripts are not showing on network tab.
– Mahi
Nov 22 at 20:29
Kinda wierd, have you tried to put ur script on header and see if its actully loading? becouse that wierd what your saying.
– Talg123
Nov 26 at 18:25
Kinda wierd, have you tried to put ur script on header and see if its actully loading? becouse that wierd what your saying.
– Talg123
Nov 26 at 18:25
add a comment |
From all the testing I have done it seems that the final attempt that you tried works flawlessly when using [0] instead of [6] or any other value for that matter. Also why do you have multiple head tags? was this a typo and you meant header? it seems to work with any number of headers.
But really this type of thing should be handled via backend if possible.
Sorry, it was a typo. Updated question. It does not help
– Mahi
Nov 30 at 4:42
add a comment |
From all the testing I have done it seems that the final attempt that you tried works flawlessly when using [0] instead of [6] or any other value for that matter. Also why do you have multiple head tags? was this a typo and you meant header? it seems to work with any number of headers.
But really this type of thing should be handled via backend if possible.
Sorry, it was a typo. Updated question. It does not help
– Mahi
Nov 30 at 4:42
add a comment |
From all the testing I have done it seems that the final attempt that you tried works flawlessly when using [0] instead of [6] or any other value for that matter. Also why do you have multiple head tags? was this a typo and you meant header? it seems to work with any number of headers.
But really this type of thing should be handled via backend if possible.
From all the testing I have done it seems that the final attempt that you tried works flawlessly when using [0] instead of [6] or any other value for that matter. Also why do you have multiple head tags? was this a typo and you meant header? it seems to work with any number of headers.
But really this type of thing should be handled via backend if possible.
answered Nov 28 at 19:31
COLBY BROOKS
873
873
Sorry, it was a typo. Updated question. It does not help
– Mahi
Nov 30 at 4:42
add a comment |
Sorry, it was a typo. Updated question. It does not help
– Mahi
Nov 30 at 4:42
Sorry, it was a typo. Updated question. It does not help
– Mahi
Nov 30 at 4:42
Sorry, it was a typo. Updated question. It does not help
– Mahi
Nov 30 at 4:42
add a comment |
It seems that you are confused with html
& javascript
.
It's impossible to use html
which is mixed up with javascript
to archive something.
With SSR(Server Side Render)
Using such as Handlebars
or another template engine.
The timestamp
(as well as your version tag) should be rendered in server side before html was generated.
<script src="/assets/abc.js?v='+new Date.getTime();" type="text/javascript"></script>
<link href="/assets/cder.css?v='+new Date.getTime();" rel="stylesheet"></link>
Without SSR(Server Side Render)
We can archive with javascript
after html
is returned by HTTP
request.
let script = document.createElement('script');
script.setAttribute('src', `somefile?v=${Date.now()}`);
document.head.appendChild(script);
It's better to wrap a function to do this job.
add a comment |
It seems that you are confused with html
& javascript
.
It's impossible to use html
which is mixed up with javascript
to archive something.
With SSR(Server Side Render)
Using such as Handlebars
or another template engine.
The timestamp
(as well as your version tag) should be rendered in server side before html was generated.
<script src="/assets/abc.js?v='+new Date.getTime();" type="text/javascript"></script>
<link href="/assets/cder.css?v='+new Date.getTime();" rel="stylesheet"></link>
Without SSR(Server Side Render)
We can archive with javascript
after html
is returned by HTTP
request.
let script = document.createElement('script');
script.setAttribute('src', `somefile?v=${Date.now()}`);
document.head.appendChild(script);
It's better to wrap a function to do this job.
add a comment |
It seems that you are confused with html
& javascript
.
It's impossible to use html
which is mixed up with javascript
to archive something.
With SSR(Server Side Render)
Using such as Handlebars
or another template engine.
The timestamp
(as well as your version tag) should be rendered in server side before html was generated.
<script src="/assets/abc.js?v='+new Date.getTime();" type="text/javascript"></script>
<link href="/assets/cder.css?v='+new Date.getTime();" rel="stylesheet"></link>
Without SSR(Server Side Render)
We can archive with javascript
after html
is returned by HTTP
request.
let script = document.createElement('script');
script.setAttribute('src', `somefile?v=${Date.now()}`);
document.head.appendChild(script);
It's better to wrap a function to do this job.
It seems that you are confused with html
& javascript
.
It's impossible to use html
which is mixed up with javascript
to archive something.
With SSR(Server Side Render)
Using such as Handlebars
or another template engine.
The timestamp
(as well as your version tag) should be rendered in server side before html was generated.
<script src="/assets/abc.js?v='+new Date.getTime();" type="text/javascript"></script>
<link href="/assets/cder.css?v='+new Date.getTime();" rel="stylesheet"></link>
Without SSR(Server Side Render)
We can archive with javascript
after html
is returned by HTTP
request.
let script = document.createElement('script');
script.setAttribute('src', `somefile?v=${Date.now()}`);
document.head.appendChild(script);
It's better to wrap a function to do this job.
answered Dec 1 at 10:21
Victor
365
365
add a comment |
add a comment |
Your script is not loading because its not new Date.getTime()
but new Date().getTime()
or just use Date.now()
. Additionally To prevent browser cache you can add no-cache header for example using .htaccess
<filesMatch ".(js|css)">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</filesMatch>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/javascript "access plus 1 seconds"
ExpiresByType text/css "access plus 1 seconds"
</IfModule>
and for javascript function
function appendJS(url) {
var s = document.createElement('script');
s.src = url + '?v=' + Date.now();
document.body.appendChild(s);
}
function appendCSS(url) {
var s = document.createElement('link');
s.rel = 'stylesheet';
s.href = url + '?v=' + Date.now();
document.body.appendChild(s);
}
appendJS('/assets/abc.js');
appendCSS('/assets/cder.css');
add a comment |
Your script is not loading because its not new Date.getTime()
but new Date().getTime()
or just use Date.now()
. Additionally To prevent browser cache you can add no-cache header for example using .htaccess
<filesMatch ".(js|css)">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</filesMatch>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/javascript "access plus 1 seconds"
ExpiresByType text/css "access plus 1 seconds"
</IfModule>
and for javascript function
function appendJS(url) {
var s = document.createElement('script');
s.src = url + '?v=' + Date.now();
document.body.appendChild(s);
}
function appendCSS(url) {
var s = document.createElement('link');
s.rel = 'stylesheet';
s.href = url + '?v=' + Date.now();
document.body.appendChild(s);
}
appendJS('/assets/abc.js');
appendCSS('/assets/cder.css');
add a comment |
Your script is not loading because its not new Date.getTime()
but new Date().getTime()
or just use Date.now()
. Additionally To prevent browser cache you can add no-cache header for example using .htaccess
<filesMatch ".(js|css)">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</filesMatch>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/javascript "access plus 1 seconds"
ExpiresByType text/css "access plus 1 seconds"
</IfModule>
and for javascript function
function appendJS(url) {
var s = document.createElement('script');
s.src = url + '?v=' + Date.now();
document.body.appendChild(s);
}
function appendCSS(url) {
var s = document.createElement('link');
s.rel = 'stylesheet';
s.href = url + '?v=' + Date.now();
document.body.appendChild(s);
}
appendJS('/assets/abc.js');
appendCSS('/assets/cder.css');
Your script is not loading because its not new Date.getTime()
but new Date().getTime()
or just use Date.now()
. Additionally To prevent browser cache you can add no-cache header for example using .htaccess
<filesMatch ".(js|css)">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</filesMatch>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/javascript "access plus 1 seconds"
ExpiresByType text/css "access plus 1 seconds"
</IfModule>
and for javascript function
function appendJS(url) {
var s = document.createElement('script');
s.src = url + '?v=' + Date.now();
document.body.appendChild(s);
}
function appendCSS(url) {
var s = document.createElement('link');
s.rel = 'stylesheet';
s.href = url + '?v=' + Date.now();
document.body.appendChild(s);
}
appendJS('/assets/abc.js');
appendCSS('/assets/cder.css');
answered Dec 1 at 14:53
ewwink
10.1k22236
10.1k22236
add a comment |
add a comment |
While other answers here correctly describe how to do this in a script, I want to note three things:
- You should do this at build-time using hashes, not timestamps, otherwise, every time the date changes your cache could be cleared. That's not a great user experience. Be intentional about when and why you're fetching this. Webpack has a nifty build plugin for creating hashes in file names.
- Use headers to drive whether a new file should be fetched.
- Make sure you're setting the
async
attribute on any run-time<script>
element, otherwise it degrades performance, sometimes significantly. Search engines penalize this heavily.
```
```
But note that appending a script tag regardless of using the async
attribute will block rendering. Read this for more details.
Hi, please give a example for better understanding
– Mahi
Dec 1 at 5:16
add a comment |
While other answers here correctly describe how to do this in a script, I want to note three things:
- You should do this at build-time using hashes, not timestamps, otherwise, every time the date changes your cache could be cleared. That's not a great user experience. Be intentional about when and why you're fetching this. Webpack has a nifty build plugin for creating hashes in file names.
- Use headers to drive whether a new file should be fetched.
- Make sure you're setting the
async
attribute on any run-time<script>
element, otherwise it degrades performance, sometimes significantly. Search engines penalize this heavily.
```
```
But note that appending a script tag regardless of using the async
attribute will block rendering. Read this for more details.
Hi, please give a example for better understanding
– Mahi
Dec 1 at 5:16
add a comment |
While other answers here correctly describe how to do this in a script, I want to note three things:
- You should do this at build-time using hashes, not timestamps, otherwise, every time the date changes your cache could be cleared. That's not a great user experience. Be intentional about when and why you're fetching this. Webpack has a nifty build plugin for creating hashes in file names.
- Use headers to drive whether a new file should be fetched.
- Make sure you're setting the
async
attribute on any run-time<script>
element, otherwise it degrades performance, sometimes significantly. Search engines penalize this heavily.
```
```
But note that appending a script tag regardless of using the async
attribute will block rendering. Read this for more details.
While other answers here correctly describe how to do this in a script, I want to note three things:
- You should do this at build-time using hashes, not timestamps, otherwise, every time the date changes your cache could be cleared. That's not a great user experience. Be intentional about when and why you're fetching this. Webpack has a nifty build plugin for creating hashes in file names.
- Use headers to drive whether a new file should be fetched.
- Make sure you're setting the
async
attribute on any run-time<script>
element, otherwise it degrades performance, sometimes significantly. Search engines penalize this heavily.
```
```
But note that appending a script tag regardless of using the async
attribute will block rendering. Read this for more details.
edited Dec 1 at 21:35
answered Dec 1 at 3:47
Jefftopia
1,09211534
1,09211534
Hi, please give a example for better understanding
– Mahi
Dec 1 at 5:16
add a comment |
Hi, please give a example for better understanding
– Mahi
Dec 1 at 5:16
Hi, please give a example for better understanding
– Mahi
Dec 1 at 5:16
Hi, please give a example for better understanding
– Mahi
Dec 1 at 5:16
add a comment |
const time = new Date(2018, 10, 24, 22); // you can provide minutes and seconds if you want
const today = new Date();
const head = document.querySelector('html head');
const body = document.querySelector('html body');
const styleFile = '<link href="/assets/cder.css?v=' + today.getTime() + '" rel="stylesheet"></link>';
const scriptFile = '<script src="/assets/abc.js?v=' + today.getTime() + '" type="text/javascript"></script>';
setInterval(() => {
if ((time.getDate() === today.getDate()) && (time.getMonth() === today.getMonth()) && (time.getFullYear() === today.getFullYear()) && (time.getHours() === today.getHours())) {
head.append(styleFile);
body.append(scriptFile);
}
}, 60000);
<html>
<head>
<!-- here we will append style file -->
</head>
<body>
<div>some dummy text</div>
<!-- here we will append script file -->
</body>
</html>
why setInterval
? that how we tell the browser "please check every minute if the condition true" if it's, so append
new styleFile and scriptFile .
Note: you can provide minutes
and seconds
in time as well if you want but remember to also add the appropriate condition in if(...){}
add a comment |
const time = new Date(2018, 10, 24, 22); // you can provide minutes and seconds if you want
const today = new Date();
const head = document.querySelector('html head');
const body = document.querySelector('html body');
const styleFile = '<link href="/assets/cder.css?v=' + today.getTime() + '" rel="stylesheet"></link>';
const scriptFile = '<script src="/assets/abc.js?v=' + today.getTime() + '" type="text/javascript"></script>';
setInterval(() => {
if ((time.getDate() === today.getDate()) && (time.getMonth() === today.getMonth()) && (time.getFullYear() === today.getFullYear()) && (time.getHours() === today.getHours())) {
head.append(styleFile);
body.append(scriptFile);
}
}, 60000);
<html>
<head>
<!-- here we will append style file -->
</head>
<body>
<div>some dummy text</div>
<!-- here we will append script file -->
</body>
</html>
why setInterval
? that how we tell the browser "please check every minute if the condition true" if it's, so append
new styleFile and scriptFile .
Note: you can provide minutes
and seconds
in time as well if you want but remember to also add the appropriate condition in if(...){}
add a comment |
const time = new Date(2018, 10, 24, 22); // you can provide minutes and seconds if you want
const today = new Date();
const head = document.querySelector('html head');
const body = document.querySelector('html body');
const styleFile = '<link href="/assets/cder.css?v=' + today.getTime() + '" rel="stylesheet"></link>';
const scriptFile = '<script src="/assets/abc.js?v=' + today.getTime() + '" type="text/javascript"></script>';
setInterval(() => {
if ((time.getDate() === today.getDate()) && (time.getMonth() === today.getMonth()) && (time.getFullYear() === today.getFullYear()) && (time.getHours() === today.getHours())) {
head.append(styleFile);
body.append(scriptFile);
}
}, 60000);
<html>
<head>
<!-- here we will append style file -->
</head>
<body>
<div>some dummy text</div>
<!-- here we will append script file -->
</body>
</html>
why setInterval
? that how we tell the browser "please check every minute if the condition true" if it's, so append
new styleFile and scriptFile .
Note: you can provide minutes
and seconds
in time as well if you want but remember to also add the appropriate condition in if(...){}
const time = new Date(2018, 10, 24, 22); // you can provide minutes and seconds if you want
const today = new Date();
const head = document.querySelector('html head');
const body = document.querySelector('html body');
const styleFile = '<link href="/assets/cder.css?v=' + today.getTime() + '" rel="stylesheet"></link>';
const scriptFile = '<script src="/assets/abc.js?v=' + today.getTime() + '" type="text/javascript"></script>';
setInterval(() => {
if ((time.getDate() === today.getDate()) && (time.getMonth() === today.getMonth()) && (time.getFullYear() === today.getFullYear()) && (time.getHours() === today.getHours())) {
head.append(styleFile);
body.append(scriptFile);
}
}, 60000);
<html>
<head>
<!-- here we will append style file -->
</head>
<body>
<div>some dummy text</div>
<!-- here we will append script file -->
</body>
</html>
why setInterval
? that how we tell the browser "please check every minute if the condition true" if it's, so append
new styleFile and scriptFile .
Note: you can provide minutes
and seconds
in time as well if you want but remember to also add the appropriate condition in if(...){}
const time = new Date(2018, 10, 24, 22); // you can provide minutes and seconds if you want
const today = new Date();
const head = document.querySelector('html head');
const body = document.querySelector('html body');
const styleFile = '<link href="/assets/cder.css?v=' + today.getTime() + '" rel="stylesheet"></link>';
const scriptFile = '<script src="/assets/abc.js?v=' + today.getTime() + '" type="text/javascript"></script>';
setInterval(() => {
if ((time.getDate() === today.getDate()) && (time.getMonth() === today.getMonth()) && (time.getFullYear() === today.getFullYear()) && (time.getHours() === today.getHours())) {
head.append(styleFile);
body.append(scriptFile);
}
}, 60000);
<html>
<head>
<!-- here we will append style file -->
</head>
<body>
<div>some dummy text</div>
<!-- here we will append script file -->
</body>
</html>
const time = new Date(2018, 10, 24, 22); // you can provide minutes and seconds if you want
const today = new Date();
const head = document.querySelector('html head');
const body = document.querySelector('html body');
const styleFile = '<link href="/assets/cder.css?v=' + today.getTime() + '" rel="stylesheet"></link>';
const scriptFile = '<script src="/assets/abc.js?v=' + today.getTime() + '" type="text/javascript"></script>';
setInterval(() => {
if ((time.getDate() === today.getDate()) && (time.getMonth() === today.getMonth()) && (time.getFullYear() === today.getFullYear()) && (time.getHours() === today.getHours())) {
head.append(styleFile);
body.append(scriptFile);
}
}, 60000);
<html>
<head>
<!-- here we will append style file -->
</head>
<body>
<div>some dummy text</div>
<!-- here we will append script file -->
</body>
</html>
answered Nov 24 at 20:29
Amir Fawzy
2188
2188
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436768%2fappend-dynamic-versionvariable-in-script-tag-and-stylesheet-based-on-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Is date and time necessary? I have a similar solution but using version number as apposed to date and time
– Luke Walker
Nov 27 at 11:42
Can you please show me it the variable should be dynamic and should not match with the previous variable value once script or stylesheet is loaded.
– Mahi
Nov 27 at 11:47
Ah i see. My version works well for production use for every time i push code. Not on a
reload
basis.– Luke Walker
Nov 27 at 11:48
do you need this for your production version to prevent caching in new versions of your website or just for development? if you specify for what purpose you want this I can help you.
– molikh
Nov 30 at 8:52
@molikh, I am wondering, the above tag resides in index.html and does it matter if this index.html is for production or development. If you can help me in either, this may solve my problem
– Mahi
Nov 30 at 9:19