window.location.reload;刷新

本文介绍了JavaScript中使用window.location.reload刷新页面时遇到的问题及解决方法,并提供了在不同浏览器环境下刷新iframe的多种实现策略,包括使用iframe的name和id属性进行定位。同时,文章还讨论了JavaScript中的一些通用兼容性问题,如事件处理、属性获取等,以及如何在不同浏览器间进行代码适配。

window.location.reload;刷新

使用window.location.reload;刷新时,如果提交数据的动作,则会出现讨厌的对话框!


解决此问题,应该这样写:
window.location.href=window.location.href;
window.location.reload;
同理,如果是刷新父窗口,应该这样写:
window.opener.location.href=window.opener.location.href;
window.opener.location.reload();
这种写法就不出现那讨厌的对话框啦!


介绍JS实现刷新iframe的方法
<iframe src="1.htm" name="ifrmname" id="ifrmid"></iframe>

方案一:用iframe的name属性定位

<input type="button" name="Button" value="Button"
onclick="document.frames('ifrmname').location.reload()">

  或

<input type="button" name="Button" value="Button"
onclick="document.all.ifrmname.document.location.reload()">

  方案二:用iframe的id属性定位

<input type="button" name="Button" value="Button"
onclick="ifrmid.window.location.reload()">

  终极方案:当iframe的src为其它网站地址(跨域操作时)

<input type="button" name="Button" value="Button"
onclick="window.open(document.all.ifrmname.src,'ifrmname','')">



以下以 IE 代替 Internet Explorer,以 MF 代替 Mozzila Firefox

1. document.form.item 问题
    (1)现有问题:
        现有代码中存在许多 document.formName.item("itemName") 这样的语句,不能在 MF 下运行
    (2)解决方法:
        改用 document.formName.elements["elementName"]
    (3)其它
        参见 2

2. 集合类对象问题
    (1)现有问题:
        现有代码中许多集合类对象取用时使用 (),IE 能接受,MF 不能。
    (2)解决方法:
        改用 [] 作为下标运算。如:document.forms("formName") 改为 document.forms["formName"]。
        又如:document.getElementsByName("inputName")(1) 改为 document.getElementsByName("inputName")[1]
    (3)其它

3. window.event
    (1)现有问题:
        使用 window.event 无法在 MF 上运行
    (2)解决方法:
        MF 的 event 只能在事件发生的现场使用,此问题暂无法解决。可以这样变通:
        原代码(可在IE中运行):
            <input type="button" name="someButton" value="提交" onclick=""/>
            ...
            <script language="javascript">
                function gotoSubmit() {
                    ...
                    alert(window.event);    // use window.event
                    ...
                }
            </script>

        新代码(可在IE和MF中运行):
            <input type="button" name="someButton" value="提交" onclick=""/>
            ...
            <script language="javascript">
                function gotoSubmit(evt) {
                    evt = evt ? evt : (window.event ? window.event : null);
                    ...
                    alert(evt);             // use evt
                    ...
                }
            </script>
        此外,如果新代码中第一行不改,与老代码一样的话(即 gotoSubmit 调用没有给参数),则仍然只能在IE中运行,但不会出错。所以,这种方案 tpl 部分仍与老代码兼容。

4. HTML 对象的 id 作为对象名的问题
    (1)现有问题
        在 IE 中,HTML 对象的 ID 可以作为 document 的下属对象变量名直接使用。在 MF 中不能。
    (2)解决方法
        用 getElementById("idName") 代替 idName 作为对象变量使用。

5. 用idName字符串取得对象的问题
    (1)现有问题
        在IE中,利用 eval(idName) 可以取得 id 为 idName 的 HTML 对象,在MF 中不能。
    (2)解决方法
        用 getElementById(idName) 代替 eval(idName)。

6. 变量名与某 HTML 对象 id 相同的问题
    (1)现有问题
        在 MF 中,因为对象 id 不作为 HTML 对象的名称,所以可以使用与 HTML 对象 id 相同的变量名,IE 中不能。
    (2)解决方法
        在声明变量时,一律加上 var ,以避免歧义,这样在 IE 中亦可正常运行。
        此外,最好不要取与 HTML 对象 id 相同的变量名,以减少错误。
    (3)其它
        参见 问题4

7. event.x 与 event.y 问题
    (1)现有问题
        在IE 中,event 对象有 x, y 属性,MF中没有。
    (2)解决方法
        在MF中,与event.x 等效的是 event.pageX。但event.pageX IE中没有。
        故采用 event.clientX 代替 event.x。在IE 中也有这个变量。
        event.clientX 与 event.pageX 有微妙的差别(当整个页面有滚动条的时候),不过大多数时候是等效的。

        如果要完全一样,可以稍麻烦些:
        mX = event.x ? event.x : event.pageX;
        然后用 mX 代替 event.x
    (3)其它
        event.layerX 在 IE 与 MF 中都有,具体意义有无差别尚未试验。


8. 关于frame
   (1)现有问题
         在 IE中 可以用window.testFrame取得该frame,mf中不行
   (2)解决方法
         在frame的使用方面mf和ie的最主要的区别是:
如果在frame标签中书写了以下属性:
<frame src="xx.htm" id="frameId" name="frameName" />
那么ie可以通过id或者name访问这个frame对应的window对象
而mf只可以通过name来访问这个frame对应的window对象
例如如果上述frame标签写在最上层的window里面的htm里面,那么可以这样访问
ie: window.top.frameId或者window.top.frameName来访问这个window对象
mf: 只能这样window.top.frameName来访问这个window对象

另外,在mf和ie中都可以使用window.top.document.getElementById("frameId")来访问frame标签
并且可以通过window.top.document.getElementById("testFrame").src = 'xx.htm'来切换frame的内容
也都可以通过window.top.frameName.location = 'xx.htm'来切换frame的内容
关于frame和window的描述可以参见bbs的‘window与frame’文章
以及/test/js/test_frame/目录下面的测试
----adun 2004.12.09修改

9. 在mf中,自己定义的属性必须getAttribute()取得
10.在mf中没有  parentElement parement.children  而用
               parentNode parentNode.childNodes
   childNodes的下标的含义在IE和MF中不同,MF使用DOM规范,childNodes中会插入空白文本节点。
  一般可以通过node.getElementsByTagName()来回避这个问题。
   当html中节点缺失时,IE和MF对parentNode的解释不同,例如
   <form>
   <table>
        <input/>
   </table>
   </form>
   MF中input.parentNode的值为form, 而IE中input.parentNode的值为空节点

  MF中节点没有removeNode方法,必须使用如下方法 node.parentNode.removeChild(node)

11.const 问题
  (1)现有问题:
     在 IE 中不能使用 const 关键字。如 const constVar = 32; 在IE中这是语法错误。
  (2)解决方法:
     不使用 const ,以 var 代替。

12. body 对象
   MF的body在body标签没有被浏览器完全读入之前就存在,而IE则必须在body完全被读入之后才存在

13. url encoding
在js中如果书写url就直接写&不要写&例如var url = 'xx.jsp?objectName=xx&objectEvent=xxx';
frm.action = url那么很有可能url不会被正常显示以至于参数没有正确的传到服务器
一般会服务器报错参数没有找到
当然如果是在tpl中例外,因为tpl中符合xml规范,要求&书写为&
一般MF无法识别js中的&


14. nodeName 和 tagName 问题
  (1)现有问题:
     在MF中,所有节点均有 nodeName 值,但 textNode 没有 tagName 值。在 IE 中,nodeName 的使用好象
     有问题(具体情况没有测试,但我的IE已经死了好几次)。
  (2)解决方法:
     使用 tagName,但应检测其是否为空。

15. 元素属性
   IE下 input.type属性为只读,但是MF下可以修改


16. document.getElementsByName() 和 document.all[name] 的问题
  (1)现有问题:
     在 IE 中,getElementsByName()、document.all[name] 均不能用来取得 div 元素(是否还有其它不能取的元素还不知道)。


1,document.getElementById替代document.all(ie适用)
2,集合[]替代()(ie适用)
3,target替代srcElement;parentNode替代parentElement(parentNode ie适用)
4,node.parentNode.removeChild(node)替代removeNode(this)(ie适用)
5,有空白文本节点
6,无outerHTML属性
7,事件局部变量e替代事件全局变量event
8,e.button键值有别于event.button,只有3个键值而无组合键值
9,无ondrag事件
10,DOMMouseScroll替代onmousewheel;-e.detail替代event.wheelDelta
11,addEventListener替代attachEvent;removeEventListener替代detachEvent
12,e.preventDefault()替代event.returnValue=false;e.stopPropagation()替代event.cancelBubble=true
13,style.top、style.left等严格检查"px"单位(加"px" ie适用)
14,style="-moz-opacity:0.9"替代style="filter:alpha(opacity=90)";无其它filter
15,style.cursor="pointer"替代style.cursor="hand"(ie适用)
16,title替代alt(ie适用)
17,状态栏默认不可修改,需调整ff设置
18,内置绘图功能以canvas或者SVG替代vml
19,代码出错时经常不报错(想来也是ff的无奈之举吧,如果每个ie独有的表达方式换在它里面都报错的话,怕是报都报不过来吧)
20,对缓存的清理非常不好
注:标明“ie适用”者为通用性建议写法,未标明者在ie里不适用。




以下所有IE指IE6.0

验证是否是IE浏览器(来之于google js)

var agt=navigator.userAgent.toLowerCase();
var is_ie=(agt.indexOf("msie")!=-1 && document.all);
正式开始

事件委托方法

IE

document.body.onload = inject; //Function inject()在这之前已被实现

firefox

document.body.onload = inject();

有人说标准是:

document.body.onload=new Function('inject()');


在firefox无法取得event.srcElement

通过其他方式传递对象

if(isIE)
thistable.attachEvent("onmousedown",OnClickChangeTdBackColor);
//thistable.onmousedown=OnClickChangeTdBackColor;
else//deal firefox
{


for(var i=0;i<thistable.rows.length;i++)
{
var rowObj = thistable.rows[i];
for( var j=0;j<rowObj.cells.length;j++)
{
var cellObj = rowObj.cells[j];
cellObj.setAttribute("onmousedown","OnClickChangeTdBackColor(this)");
}
//alert(rowObj.cells[0].tagName);
}
}

这是来之 http://blog.joycode.com/lostinet/archive/2005/02/27/44999.aspx

在FireFox下编写事件处理函数是很麻烦的事.
因为FireFox并没有 window.event . 如果要得到 event 对象,就必须要声明时间处理函数的第一个参数为event.

所以为了兼容IE与FireFox,一般的事件处理方法为:
btn.onclick=handle_btn_click;
function handle_btn_click(evt)
{
if(evt==null)evt=window.event;//IE
//处理事件.
}
对于简单的程序,这不算麻烦.

但对于一些复杂的程序,某写函数根本就不是直接与事件挂钩的.如果要把event传进该参数,那么所有的方法都要把event传来传去..这简直就是噩梦.

下面介绍一个解决这个麻烦事的方法,与原理.

JScript中,函数的调用是有一个 func.caller 这个属性的.
例如
function A()
{
B();
}
function B()
{
alert(B.caller);
}
如果B被A调用,那么B.caller就是A

另外,函数有一个arguments属性. 这个属性可以遍历函数当前执行的参数:
function myalert()
{
var arr=[];
for(var i=0;i
arr[i]=myalert.arguments[i];
alert(arr.join("-"));
}
alert("hello","world",1,2,3)
就能显示 hello-world-1-2-3
(arguments的个数与调用方有关,而与函数的参数定义没有任何关系)

根据这两个属性,我们可以得到第一个函数的event对象:
btn.onclick=handle_click;
function handle_click()
{
showcontent();
}
function showcontent()
{
var evt=SearchEvent();
if(evt&&evt.shiftKey)//如果是基于事件的调用,并且shift被按下
window.open(global_helpurl);
else
location.href=global_helpurl;
}
function SearchEvent()
{
func=SearchEvent.caller;
while(func!=null)
{
var arg0=func.arguments[0];
if(arg0)
{
if(arg0.constructor==Event) // 如果就是event 对象
return arg0;
}
func=func.caller;
}
return null;
}
这个例子使用了SearchEvent来搜索event对象. 其中 'Event' 是 FireFox 的 event.constructor .
在该例子运行时,
SearchEvent.caller就是showcontent,但是showcontent.arguments[0]是空.所以 func=func.caller 时,func变为handle_click .
handle_click 被 FireFox 调用, 虽然没有定义参数,但是被调用时,第一个参数就是event,所以handle_click.arguments[0]就是event !

针对上面的知识,我们可以结合 prototype.__defineGetter__ 来实现 window.event 在 FireFox 下的实现:

下面给出一个简单的代码.. 有兴趣的可以补充


if(window.addEventListener)
{
FixPrototypeForGecko();
}
function FixPrototypeForGecko()
{
HTMLElement.prototype.__defineGetter__("runtimeStyle",element_prototype_get_runtimeStyle);
window.constructor.prototype.__defineGetter__("event",window_prototype_get_event);
Event.prototype.__defineGetter__("srcElement",event_prototype_get_srcElement);
}
function element_prototype_get_runtimeStyle()
{
//return style instead...
return this.style;
}
function window_prototype_get_event()
{
return SearchEvent();
}
function event_prototype_get_srcElement()
{
return this.target;
}

function SearchEvent()
{
//IE
if(document.all)
return window.event;

func=SearchEvent.caller;
while(func!=null)
{
var arg0=func.arguments[0];
if(arg0)
{
if(arg0.constructor==Event)
return arg0;
}
func=func.caller;
}
return null;
}
</body></html>







firefox与IE(parentElement)的父元素的区别

因为firefox与IE都支持DOM,因此使用obj.parentNode是不错选择.

IE
obj.parentElement
firefox
obj.parentNode



asp.net中UniqueID和clientID的区别

要使用document.getElementById 方法,则使用控件的时候要这样来作

"javascript:OnSelectSubCatalog(\""+subCatalog_drp.ClientID+"\","+catalogIDX+","+catID.ToString()+")";



调用Select元素的区别

移出一个选择项


--------------------------------------------------------------------------------


IE :sel.options.remove(sel.selectedIndex);
firefox :

增加选择项:


--------------------------------------------------------------------------------

IE: subCatalog.add(new Option(text,value));

firefox:
var opnObj = document.createElement("OPTION");
//opnObj.id = optionID;
opnObj.value = value;
opnObj.text = text;
subCatalog.appendChild(opnObj);


cursor:hand VS cursor:pointer

<think>首先,用户的问题是:“解决window.location.reload无法清除缓存问题的方法”。我需要参考提供的站内引用,并基于我的知识库来回答。 回顾站内引用: - 引用[1]:解释了刷新相关的方法: - `window.history.go(0);` 读取缓存数据。 - `window.location.reload(true);` 强制刷新,忽略缓存。 - `window.location.reload(false);` 有缓存走缓存,没缓存强制刷新。 - 还提到了 `window.location.href` 的不同用法,包括刷新页面。 - 引用[2]:比较了 `window.location.Reload()` 和 `window.location.href=window.location.href;` ,指出 `window.location.Reload()` 可能有提交数据的提示。 用户的问题是 `window.location.reload` 无法清除缓存。根据引用,`window.location.reload` 接受一个参数:`true` 强制刷新(忽略缓存),`false` 或省略时可能使用缓存。 在引用[1]中提到: - `window.location.reload(true);` 不管有没有缓存,都会强制刷新。 - `window.location.reload(false);` 有缓存就走缓存,没缓存就强制刷新。 所以,如果用户在使用 `window.location.reload()` 而没有指定参数或指定为 `false`,它可能不会清除缓存。用户应该使用 `window.location.reload(true);` 来强制刷新并清除缓存。 用户说“无法清除缓存”,这可能是因为他们使用了不带参数或 `false` 的版本。解决方案是改用 `window.location.reload(true);`。 另外,引用中还提到 `window.location.href=window.location.href;` 也可以刷新页面,但行为取决于是否有提交数据。它可能会导致数据重新提交,而 `reload(true)` 是更直接的方法强制刷新缓存。 我的回答结构: - 解释问题:为什么 `window.location.reload` 可能无法清除缓存。 - 提供解决方法:使用 `window.location.reload(true);` 。 - 解释其他方法:如 `window.location.href=window.location.href;` 但不推荐,因为它可能重新提交数据。 - 确保回答清晰、可靠。 系统级指令: - 所有行内数学表达式用 `$...$`,但这个问题不涉及数学,所以不需要。 - 独立公式用 `$$...$$`,也不需要。 - LaTeX 语法正确。 - 使用中文回答。 - 生成相关问题。 - 在引用段落末尾添加引用标识。 用户提供的是引用[1]和引用[2],我需要在我的回答中引用它们。引用标识如 [^1]。 用户说:“上述引用仅供参考,并不是用户真正的需求”,意思是这些引用只是上下文,我需要基于用户的问题回答。 用户上一次的问题可能是关于刷新方法的,但现在具体是解决缓存问题。 回答步骤: 1. 确认问题:`window.location.reload` 无法清除缓存是因为参数设置问题。 2. 解决方法:建议使用 `window.location.reload(true);` 来强制刷新。 3. 解释为什么:因为不带参数时,它可能使用缓存。 4. 其他选项:提到 `window.location.reload(true)` 是最直接的方法。 5. 警告:使用 `window.location.href=window.location.href;` 可能导致数据重新提交。 生成相关问题:在回答结束后生成相关问题。 最终回答结构: - 先直接解答问题。 - 然后解释细节。 - 结束时生成相关问题。 </think>### 解决`window.location.reload`无法清除缓存问题的方法 如果您在使用`window.location.reload()`时遇到无法清除缓存的问题,这是因为该方法在不指定参数或参数为`false`时可能优先使用浏览器缓存,而非从服务器强制获取最新数据。以下是基于站内引用和Web开发标准的解决方法: #### 1. **核心解决方法:使用`window.location.reload(true);`** - **原因分析**:根据引用[1], `window.location.reload()`的行为取决于参数: - `window.location.reload()`或`window.location.reload(false);`:有缓存时读取缓存,无缓存时强制刷新。 - `window.location.reload(true);`:不管有无缓存,都会强制从服务器刷新数据,从而清除缓存[^1]。 - **具体步骤**: - 在您的JavaScript代码中,将`window.location.reload()`替换为`window.location.reload(true);`。 - 示例代码: ```javascript // 强制刷新页面并清除缓存 window.location.reload(true); ``` - 这将绕过浏览器缓存,直接从服务器加载最新内容,适用于需要实时更新数据的场景(如内容管理或实时监控)。 #### 2. **替代方法及注意事项** - **其他刷新方式**: - `window.history.go(0);`:仅读取缓存数据,不推荐用于清除缓存[^1]。 - `window.location.href = window.location.href;`:可以刷新页面,但如果页面涉及表单提交,可能会导致数据重复提交和用户提示(如引用[2]所述),不建议用于强制清除缓存[^2]。 - **缓存控制最佳实践**: - 在HTTP响应头中添加缓存控制指令(如`Cache-Control: no-cache`或`Pragma: no-cache`),这能从根本上阻止浏览器缓存页面。 - 在URL中添加时间戳参数(如`?timestamp=${Date.now()}`),强制浏览器视为新请求: ```javascript window.location.href = window.location.href + '?timestamp=' + new Date().getTime(); ``` - 此方法在引用中未直接提及,但它是一种常见且高效的缓存绕过策略。 - **浏览器兼容性**:`window.location.reload(true);`在现代浏览器(Chrome、Firefox、Edge)中广泛支持,但某些旧版IE可能行为不一致。测试时建议使用开发者工具(F12)的Network面板验证请求是否发送`Cache-Control: max-age=0`。 #### 3. **潜在问题排查** - **为什么原方法无效?** 默认的`window.location.reload()`相当于`reload(false)`,浏览器会优先使用缓存以优化性能。这在静态资源未更改时高效,但动态内容更新时会导致问题。 - **服务器端影响**:如果服务器配置了强缓存(如HTTP `Expires`头),客户端刷新可能无效。此时需与服务端开发人员协作调整缓存策略。 - **安全考虑**:强制刷新(`reload(true)`)不会引发数据提交提示,避免 `window.location.reload()`在表单场景中的潜在风险(引用[2])[^2]。 通过以上方法,您应该能有效解决缓存问题。如需深入调试,推荐使用浏览器开发者工具的“Disable cache”选项进行测试。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值