静态HTML页面不缓存js文件的方法
今天做项目时候遇到一个问题,由于采用了生成静态的CMS系统,但是页面头部需要显示用户登录的信息,也就是,没有登录时,显示登录框,用户登录后,则显 示登录信息。于是用到了js调用php文件的方法。但是由于浏览器的缓存,用户登录后常常还是显示登录框,因为js文件被缓存,没有重新下载。
由于js文件是用<script>标签引入的,无法加随机数参数以使每次都重新下载。经过研究采用以下方法达到目的:
这里是头部的html代码:
<table width="770" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="377" height="35" align="left" nowrap="nowrap" id="user_status_div">
<script type="text/javascript">
var jsFile = document.createElement("script");
jsFile.setAttribute("type","text/javascript");
jsFile.setAttribute("src","/member/user_status_js.php?random="+Math.random());
document.getElementById('user_status_div').appendChild(jsFile);
</script>
</td>
<td width="393" align="right" nowrap="nowrap"><a href="http://www.nmc.gov.cn/weatherdetail/57494.html" title="点击查看详细天气预报" target="_blank">武汉</a><span style="font-size: 9pt">
<script src="/weather.php"></script>
</span><span> <a href="http://www.nmc.gov.cn/weatherdetail/57494.html" title="点击查看详细天气预报" target="_blank">更多</a> <a
onclick="this.style.behavior='url(#default#homepage)';this.sethomepage('http://www.cnmamia.com');return false;"
href="http://www.qg.org.cn#">设为首页</a> <a href='#' onClick="javascript:window.external.AddFavorite('{dede:global name="cfg_basehost"/}','{dede:global name="cfg_webname"/}');">收藏本站</a></span></td>
</tr>
</table>
这里是生成调用js的php文件:
<?php
if(!isset($_COOKIE['DedeUserID'])||$_COOKIE['DedeUserID']=='')
{
?>
document.getElementById("user_status_div").innerHTML='<form id="form1" name="form1" method="post" action="/member/index_do.php">[<a href="/member/index_do.php?fmdo=user&dopost=regnew">会员注册</a>] 用户名<input type="text" name="userid" id="userid" class="user" /> 密码<input name="pwd" type="password" class="user" /> <input type="submit" name="btnsubmit" id="btnsubmit" value="登录" class="login" /><input type="hidden" name="fmdo" value="login"><input type="hidden" name="dopost" value="login"><input type="hidden" name="gourl" id="gourl" value="'+window.location.href+'"><input name="nochvd" type="hidden" id="nochvd" value="1" /></form>';
<?php
}
else
{
require_once(dirname(__FILE__)."/config.php");
require_once(dirname(__FILE__)."/../include/config_base.php"); $dsql = new DedeSql(false);
$query="Select userid From #@__member where id=".$_COOKIE['DedeUserID'];
$username = $dsql->GetOne($query);
$dsql->Close();
?>
document.getElementById("user_status_div").innerHTML='欢迎您 <?php echo($username['userid']);?> [<a href="/member/index.php">用户中心</a>] [<a href="/member/index.php?uid=<?php echo($username['userid']);?>">我的空间</a>] [<a href="/member/index_do.php?fmdo=login&dopost=exit&forward='+window.location.href+'">退出登录</a>]';
<?php
}
?>
注意这里采用了crateElement方法来动态创建<script>标签,达到了添加随机参数的目的。另外,在调用的js文件中,必须采用innerHTML方法,而不是直接document.write,否则排版可能会不正确。
转载:http://www.cnblogs.com/webStyle/p/3643718.html
静态HTML页面不缓存js文件的方法的更多相关文章
- 让html页面不缓存js的实现方法
很多朋友都会碰到这样的情况:如果我们页面加载了js的话下次打开时也会是调用这个js缓存文件,但对于我们调试时是非常的不方便了,本文就来谈论如何解决这一问题,下面一起来看看. 不缓存JS的方法其实挺简单 ...
- JS跨页面或跨JS文件对变量赋值
JS跨页面或跨JS文件对变量赋值,这是很小的一个问题. 但问题虽小,却总觉得有点不够自然,不爽. 为什么呢?访问一个页面上的变量不是什么难事,比如用parent.变量名,或者windows名.变量名, ...
- vue页面引入外部js文件遇到的问题
问题一:vue文件中引入外部js文件的方法 //在vue文件中 <script> import * as funApi from '../../../publicJavaScript/pu ...
- 异步加载js文件的方法
# 异步加载js文件 - js的加载默认是同步的,因为js是单线程执行,只能完成一件再执行下一件. - 一些外部引入的js文件可以因为文件太大,在加载资源的过程中会影响dom元素的加载,影响了用户体验 ...
- 过滤器会拦截 前端页面加载 js文件的请求
学艺不精啊.....之前就总结过博客: JAVA中解决Filter过滤掉css,js,图片文件等问题 结果现在又犯了老错误~ 情况如下: index.jsp 页面的验证码输入栏绑定了异步验证(jQur ...
- nuxt 头部引入js文件 第一次进入页面不加载js文件的解决方法
head () { return { title: '', meta: [ { hid: 'description', name: 'description', content: '' } ], sc ...
- 异步加载js文件的方法总结
方法一,jQuery.getScript HTML 代码: 代码如下 复制代码 <button id="go">Run</button><div cl ...
- js动态加载css文件和js文件的方法
今天研究了下js动态加载js文件和css文件的方法. 网上发现一个动态加载的方法.摘抄下来,方便自己以后使用 [code lang="html"] <html xmlns=& ...
- HTML5的兼容问题以及调用js文件的方法
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
随机推荐
- 用java将excel表格中的内容修改后写入到另一个excel中
package nn; import java.io.File; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl. ...
- Codeforces Round #303 (Div. 2) C dp 贪心
C. Woodcutters time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- jquery中prop()方法和attr()方法的区别
最近在用jquery的时候遇到一个问题,那就是attr()方法,发现这个方法有时候使用会有一些说不出原因的问题.翻翻自己之前笔记发现,还有个函数prop(). 这两个函数都可以用来获取属性. jque ...
- c 函数及指针学习 6
不完整声明 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 /* 方法一 */ struct tag_a{ ...
- MongoDB安装、配置和基本使用
一.搭建MongoDB的yum源 如果安装epel扩展源,可以安装MongoDB2.4版本 如要安装3.0版本,需要自己配置yum源,官方提供的源只支持64位系统 # vim /etc/yum.rep ...
- Python-正则零宽断言及命名捕获(类PHP)
(一)零宽断言 说明:本文的例子使用python描述 首先说明一下什么是零宽断言,所谓零宽断言就是并不去真正的匹配字符串文本,而仅仅是匹配对应的位置. 正则表达式中有很多这样的断言 ...
- js调用百度地图API创建地图,搜索位置
实现代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <met ...
- linux日志处理logrotate使用
摘录自:http://linux008.blog.51cto.com/2837805/555829 内容在这里做个备份,以便以后查看: 使用logrotate管理nginx日志文件 2011-04- ...
- jQuery根据文本设置select选中失效问题
$("#select_id option[text='jQuery']").attr("selected", true) 失效 text是Jquery关键字,不 ...
- 【转】javascript弹出固定大小的窗口页面
来源:http://weicfprince.blog.163.com/blog/static/8441066920081010113231969/ 现在我们想弹出一个WEB窗体,让其处于屏幕的中间位置 ...