---
title: "静态HTML页面不缓存js文件的方法"
author: "lonestone"
date: "2007-11-17T12:14:00Z"
tags: []
slug: "html-js"
draft: false
lastmod: "2007-11-17T12:14:00Z"
id: 897807
url: "https://lonestone.iteye.com/blog/897807"
---

今天做项目时候遇到一个问题，由于采用了生成静态的CMS系统，但是页面头部需要显示用户登录的信息，也就是，没有登录时，显示登录框，用户登录后，则显示登录信息。于是用到了js调用php文件的方法。但是由于浏览器的缓存，用户登录后常常还是显示登录框，因为js文件被缓存，没有重新下载。

由于js文件是用&lt;script&gt;标签引入的，无法加随机数参数以使每次都重新下载。经过研究采用以下方法达到目的：

这里是头部的html代码：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;tablewidth="770"border="0"align="center"cellpadding="0"cellspacing="0"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;tr&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;tdwidth="377"height="35"align="left"nowrap="nowrap"id="user\_status\_div"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)&lt;scripttype="text/javascript"&gt;...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)varjsFile=document.createElement("script");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)jsFile.setAttribute("type","text/javascript");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)jsFile.setAttribute("src","/member/user\_status\_js.php?random="+Math.random());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)document.getElementById('user\_status\_div').appendChild(jsFile);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)&lt;/script&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/td&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;tdwidth="393"align="right"nowrap="nowrap"&gt;&lt;ahref="http://www.nmc.gov.cn/weatherdetail/57494.html"title="点击查看详细天气预报"target="\_blank"&gt;武汉&lt;/a&gt;&lt;spanstyle="font-size:9pt"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;scriptsrc="/weather.php"&gt;&lt;/script&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/span&gt;&lt;span&gt;&nbsp;&lt;ahref="http://www.nmc.gov.cn/weatherdetail/57494.html"title="点击查看详细天气预报"target="\_blank"&gt;更多&lt;/a&gt;&nbsp;&nbsp;&lt;a  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)onclick="this.style.behavior='url(#default#homepage)';this.sethomepage('http://www.cnmamia.com');returnfalse;"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)href="http://www.qg.org.cn#"&gt;设为首页&lt;/a&gt;&nbsp;&lt;ahref='#'onClick="javascript:window.external.AddFavorite('{dede:globalname="cfg\_basehost"/}','{dede:globalname="cfg\_webname"/}');"&gt;收藏本站&lt;/a&gt;&lt;/span&gt;&lt;/td&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/tr&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/table&gt;

这里是生成调用js的php文件：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;?php  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)if(!isset($\_COOKIE\['DedeUserID'])||$\_COOKIE\['DedeUserID']=='')  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif){  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)?&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)document.getElementById("user\_status\_div").innerHTML='&lt;formid="form1"name="form1"method="post"action="/member/index\_do.php"&gt;\[&lt;ahref="/member/index\_do.php?fmdo=user&dopost=regnew"&gt;会员注册&lt;/a&gt;]&nbsp;用户名&lt;inputtype="text"name="userid"id="userid"class="user"/&gt;&nbsp;密码&lt;inputname="pwd"type="password"class="user"/&gt;&nbsp;&lt;inputtype="submit"name="btnsubmit"id="btnsubmit"value="登录"class="login"/&gt;&lt;inputtype="hidden"name="fmdo"value="login"&gt;&lt;inputtype="hidden"name="dopost"value="login"&gt;&lt;inputtype="hidden"name="gourl"id="gourl"value="'+window.location.href+'"&gt;&lt;inputname="nochvd"type="hidden"id="nochvd"value="1"/&gt;&lt;/form&gt;';  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;?php  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif){  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)require\_once(dirname(\_\_FILE\_\_)."/config.php");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)require\_once(dirname(\_\_FILE\_\_)."/../include/config\_base.php");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)$dsql=newDedeSql(false);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)$query="SelectuseridFrom#@\_\_memberwhereid=".$\_COOKIE\['DedeUserID'];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)$username=$dsql-&gt;GetOne($query);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)$dsql-&gt;Close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)?&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)document.getElementById("user\_status\_div").innerHTML='欢迎您&lt;?phpecho($username\['userid']);?&gt;\[&lt;ahref="/member/index.php"&gt;用户中心&lt;/a&gt;]\[&lt;ahref="/member/index.php?uid=&lt;?phpecho($username\['userid']);?&gt;"&gt;我的空间&lt;/a&gt;]\[&lt;ahref="/member/index\_do.php?fmdo=login&dopost=exit&forward='+window.location.href+'"&gt;退出登录&lt;/a&gt;]';  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;?php  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)?&gt;

注意这里采用了crateElement方法来动态创建&lt;script&gt;标签，达到了添加随机参数的目的。另外，在调用的js文件中，必须采用innerHTML方法，而不是直接document.write，否则排版可能会不正确。

希望此雕虫小技能给你带来帮助。