<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?
Dynamic loadingCreate a script element. Set the src , async , and type attributes. Append the script element to the body. Check if the file loaded or not in the load event.
To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.
The other night, Amit Patel mentioned that you can set script tags in HTML to display: block with CSS and then edit that code inline with the contentEditable attribute. This means that you can then see it all update live in the browser as you type.
Definition and Usage The <script> tag is used to embed a client-side script (JavaScript). The <script> element either contains scripting statements, or it points to an external script file through the src attribute. Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.
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>
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With