javascript - 如何使用 JavaScript 在静态 HTML 网页上为脚本 src 字符串添加 Unix 时间戳?
问题描述
我想出了这个,但它不起作用:
<!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>
推荐阅读
- react-native - 如何在 React Native android 中将日期格式设置为从现在开始多少小时?
- ios - 如何为下面的表格视图设置正确的约束?
- swift - 如何在 swiftui 中限制对 stateobject 的更改
- kubernetes - 我可以获得 storageclasses.storage.k8s.io 支持的访问模式吗?
- sapui5 - SAPUI5 V1.38.39 自动隐藏后台模式下的图片
- php - PHP Foreach 列数据明智
- javascript - React Js超时时如何删除组件
- questdb - 如何使用 postgre jdbc 驱动程序从 questdb 读取大行?
- python-3.6 - 在python中将字符串转换为列表的最佳方法是什么
- javascript - 如何在 websocket 服务器中设置特定的 ip?(node.js)