I've come up with this, but it doesn't work:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<script src="/test.php?nocache=" + Date.now() + "></script>
</body>
</html>
It has to be part of the src
string. I believe I could do <script> console.log(Date.now()); </script>
<script> console.log(Date.now()); </script>
if all I wanted to do was to output the Unix timestamp in the browser console, but that's not what I'm trying to do. I'm trying to prevent the /test.php
(which handles logging of page loads) from being cached by browsers, and since the page itself is not being served dynamically, I have to do it client-side.
You can not execute js in HTML attribute. But you could do like this
<script>
const script=document.createElement('script');
script.src='/test.php?nocache=' + Date.now();
document.head.appendChild(script);
</script>
I suggest you load your script after the page has loaded. This way you can dynamically set your script's src
attribute:
var script = document.createElement("script");
script.setAttribute("src","/test.php?nocache=" + Date.now());
document.body.appendChild(script);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.