zoukankan      html  css  js  c++  java
  • js刷新页面

    location.reload() 和 location.replace()的区别和应用:

    reload 方法,该方法强迫浏览器刷新当前页面。
    语法: location.reload([bForceGet])     

    参数: bForceGet, 可选参数, 默认为 false,从客户端缓存里取当前页。 true, 则以 GET 方式,从服务端取最新的页面, 相当于客户端点击 F5("刷新")

    replace 方法,该方法通过指定URL替换当前缓存在历史里(客户端)的项目,因此当使用replace方法之后,你不能通过“前进”和“后退”来访问已经被替换的URL
    语法: location.replace(URL)                                                                                                     

    参数: URL

    在实际应用的时候,重新刷新页面的时候,我们通常使用: location.reload() 或者是 history.Go(0) 来做。因为这种做法就像是客户端点F5刷新页面,所以页面的method="post"的时候,会出现“网页过期”的提示。那是因为Session的安全保护机制。可以想到: 当调用 location.reload() 方法的时候, aspx页面此时在服务端内存里已经存在, 因此必定是 IsPostback 的。如果有这种应用: 我们需要重新加载该页面,也就是说我们期望页面能够在服务端重新被创建, 我们期望是 Not IsPostback 的。这里,location.replace() 就可以完成此任务。被replace的页面每次都在服务端重新生成。你可以这么写: location.replace(location.href)

    reload()方法举例

    例1,刷新当前页面

    <script> 
    window.location.reload(); 
    </script>

    例2,JS实现刷新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()">

    例3,首先,定义一个iframe 

    <iframe method="post" id ="IFrameName" src="aa.htm" ></iframe>

    aa.htm页面的内容: 

    <input type ="button" value ="刷新" onclick ="aa()"/> 
    function aa() { (jquery中文网 www.jquerycn.cn 编辑整理)
    //parent.location.replace(parent.location.href);//服务器端重新创建页面 
    parent.document.location.reload();//相当于F5 
    //window.location.href(parent.location.href);//iframe内容重定向 
    }

    注意:
    window.location.reload;
    刷新时如果提交数据的动作,则会出现对话框!

    解决办法:

    window.location.href=window.location.href; 
    window.location.reload;

    刷新父窗口:

    window.opener.location.href=window.opener.location.href; 
    window.opener.location.reload(); 

    这种写法不会显示对话框。

  • 相关阅读:
    返回顶部
    C# 对文本文件的几种读写方法
    cocos2dx 锁定30帧设置
    AndroidManifest.xml 屏幕上下反转
    粒子系统主
    CCParticleSystem粒子系统
    精灵的优化
    cocos2dx 菜单按钮回调方法传参 tag传参
    cocos2dx跨平台使用自定义字体
    ios7 Cocos2dx 隐藏状态栏设置
  • 原文地址:https://www.cnblogs.com/pengyunjing/p/6415298.html
Copyright © 2011-2022 走看看