手机端页面设置缓存

设置缓存
<meta http-equiv="Cache-Control"content="no-cache"/>
手机页面通常在第一次加载后会进行缓存,然后每次刷新会使用缓存而不是去重新向服务器发送请求。如果不希望使用缓存可以设置no-cache。

前端手机端超链接跳转缓存问题可能会导致页面显示旧数据,影响用户体验。以下是一些常见的解决方案: ### 1. 使用时间戳或随机数 在超链接的 URL 后面添加一个时间戳或随机数作为查询参数,这样每次请求的 URL 都会不同,从而避免缓存。 ```html <a href="page.html?timestamp=1645689012">跳转页面</a> ``` 可以使用 JavaScript 动态生成时间戳: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> <a id="myLink" href="page.html">跳转页面</a> <script> const link = document.getElementById('myLink'); const timestamp = new Date().getTime(); link.href = link.href + '?timestamp=' + timestamp; </script> </body> </html> ``` ### 2. 设置 HTTP 头信息 在服务器端设置响应头信息,告诉浏览器不要缓存页面。不同的服务器语言设置方式不同,以下是一些常见的示例: #### Node.js(Express) ```javascript const express = require('express'); const app = express(); app.get('/page.html', (req, res) => { res.set({ 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0' }); res.sendFile(__dirname + '/page.html'); }); const port = 3000; app.listen(port, () => { console.log(`Server running on port ${port}`); }); ``` #### PHP ```php <?php header("Cache-Control: no-cache, no-store, must-revalidate"); header("Pragma: no-cache"); header("Expires: 0"); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>页面标题</title> </head> <body> 页面内容 </body> </html> ``` ### 3. 使用 HTML meta 标签 在 HTML 页面的 `<head>` 标签中添加 meta 标签,设置缓存控制信息。 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"> <meta http-equiv="Pragma" content="no-cache"> <meta http-equiv="Expires" content="0"> <title>页面标题</title> </head> <body> 页面内容 </body> </html> ``` ### 4. 强制刷新页面 在跳转时使用 JavaScript 强制刷新页面。 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> <a id="myLink" href="page.html">跳转页面</a> <script> const link = document.getElementById('myLink'); link.addEventListener('click', function (e) { e.preventDefault(); window.location.href = this.href; location.reload(true); }); </script> </body> </html> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值