我已经想出了这个,但它不起作用:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<script src="/test.php?nocache=" + Date.now() + "></script>
</body>
</html>
它必须是src
字符串的一部分。如果我只想在浏览器控制台中输出Unix时间戳,我相信我可以执行<script> console.log(Date.now()); </script>
,但这不是我想要做的。我试图阻止浏览器缓存/test.php
(处理页面加载的日志记录(,因为页面本身不是动态提供的,所以我必须在客户端执行。
您不能在HTML属性中执行js。但你可以像这个一样
<script>
const script=document.createElement('script');
script.src='/test.php?nocache=' + Date.now();
document.head.appendChild(script);
</script>
我建议您在页面加载后加载脚本。通过这种方式,您可以动态设置脚本的src
属性:
var script = document.createElement("script");
script.setAttribute("src","/test.php?nocache=" + Date.now());
document.body.appendChild(script);