JavaScript中使用location对象可以通过很多种方式改变浏览器的位置。最常用的方法应该是下面几种:

demo.js
1
2
3
  location.href = "http://www.google.com";
  window.location = "http://www.google.com";
  location.assign("http://www.google.com");

其实前面2种方式是调用第3种方式去实现的,这样就可以在浏览器中打开一个新的URL并在历史记录中生成一条记录,因此用户可以通过“后退”按钮回到前一个页面。如果想禁止这种后退行为,可以使用replace()方法。该方法只接受一个参数,即URL,然后会跳转到该URL,但不会在历史记录中生成记录。这样用户不能回到前一页了。

demo.js
1
    location.replace("http://www.google.com");

关于页面跳转还有一个reload方法,作用是重新加载当前页面。如果调用该方法时不带参数,页面会以最有效的方式加载页面,也就是说,如果浏览器有该页面的缓存,则会从缓存里面加载该页面。如果不想从缓存中重新加载页面,需要传入true参数,即绕开浏览器缓存,强制从服务器去获取页面数据。

demo.js
1
2
  location.reload(); //相当浏览器的F5刷新,有可能从缓存中加载
location.reload(true); //相当于Ctrl+F5,从服务器中加载

下面列出location.reload()方法的具体解释:

The reload() method of the Location object reloads the document that is currently displayed in the window of the Location object. When called with no arguments, or with the argument false, it uses the If-Modified-SinceHTTP header to determine whether the document has changed on the web server. If it has, it reloads the document from the server, and if not, it reloads the document from the cache. This is is the same action that occurs when the user clicks on Navigator’s Reload button.

location对象的reload方法会重新加载当前页面。当没有传入参数或传入false参数时,会用 If-Modified-SinceHTTP header 去判断页面在web服务器上是否有改变。如果改变了,则从服务器加载,如果没有改变,则从缓存中加载。这与用户点击导航栏刷新按钮的行为相同。

When reload() is called with the argument true, then it will always bypass the cache and reload the document from the server, regardless of the last-modified time of the document. This is the same action that occurs when the user shift-clicks on Navigator’s Reload button.

当调用reload方法时传入参数true,会始终绕开缓存而从服务器上加载页面,不管页面有没有改变。这与用户按住shift并点击导航栏刷新按钮的行为相同。

Location对象的页面跳转方法介绍的更多相关文章

  1. 使用 history 对象和 location 对象中的属性和方法制作一个简易的网页浏览工具

    查看本章节 查看作业目录 需求说明: 使用 history 对象和 location 对象中的属性和方法制作一个简易的网页浏览工具 实现思路: 使用history对象中的 forward() 方法和 ...

  2. php开发中的页面跳转方法总结

    PHP页面跳转实现的功能就是将网站中一个网页跳转到另一个网页中.对于刚刚学习PHP语言的朋友来说,是必须要掌握的基础方法. 页面跳转可能是由于用户单击链接.按钮等触发的,也可能是系统自动产生的.页面自 ...

  3. JAVA-JSP内置对象之response对象实现页面跳转

    相关资料:<21天学通Java Web开发> response对象 实现页面跳转1.可以通过response对象的sendRedirect()方法设置页面重定向,从而实现页面跳转.2.这种 ...

  4. PHP中常见的页面跳转方法

    转载自冠威博客 [ http://www.guanwei.org/ ]本文链接地址:http://www.guanwei.org/post/PHPnotes/04/php-redirect-metho ...

  5. location对象的一些属性和方法

    window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面 以下是window.location的属性 window.location.host 返回主机名或者 ...

  6. window.location对象 获取页面地址

    window.location对象的属性: 属性 含义 值 location.protocol 协议 "http://"或"https://" location ...

  7. PHP 页面跳转方法

    1.php header()函数跳转 PHP的header()函数非常强大,其中在页面url跳转方面也调用简单,使用header()直接跳转到指定url页面,这时页面跳转是302重定向: $url = ...

  8. WEB开发中的页面跳转方法总结

    PHP header()函数跳转 PHP的header()函数非常强大,其中在页面url跳转方面也调用简单,使用header()直接跳转到指定url页面,这时页面跳转是302重定向: $url = & ...

  9. 微信小程序页面跳转方法和携带参数详解

    1.页面跳转方式     (1)标签跳转   open-type的属性值对应api里的用法即wx.的用法   1 <navigator url="/page/navigate/navi ...

随机推荐

  1. java-mybaits-00701-与spring整合

    1.1     整合思路   需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSes ...

  2. 一些动态规划问题的java实现

    从上往下推公式,从下往上求解值. 一:矩阵链乘法,最小括号化方案,动态规划方程. 0        如果i=j m[i,j]  ={ min[i,k]+m[k+1,j]+pi-1pkpj        ...

  3. 常用linux shell脚本记录

    遍历目录下所有的文件是目录还是文件 for file in ./* do if test -f $file then echo $file 是文件 fi if test -d $file then e ...

  4. cocos代码研究(21)Widget子类TextField学习笔记

    基础理论 一个接受用户输入的widget. 输入文本的渲染基于TextFieldTTF. 如果你想用系统控制行为,请使用EditBox来替代.继承自 Widget. 代码实践 //与占位符有关void ...

  5. sql server递归子节点、父节点,sql查询表结构,根据字段名查所在表

    一.查询当前部门下的所有子部门 WITH dept AS ( SELECT * FROM dbo.deptTab --部门表 WHERE pid = @id UNION ALL SELECT d.* ...

  6. OAuth 白话简明教程 5.其他模式

    转自:http://www.cftea.com/c/2016/11/6706.asp OAuth 白话简明教程 1.简述 OAuth 白话简明教程 2.授权码模式(Authorization Code ...

  7. RabittMQ实践(一): RabbitMQ的安装、启动

    安装:   启动监控管理器:rabbitmq-plugins enable rabbitmq_management 关闭监控管理器:rabbitmq-plugins disable rabbitmq_ ...

  8. nats

    NATS is a family of open source products that are tightly integrated but can be deployed independent ...

  9. Android查缺补漏(IPC篇)-- 进程间通讯之AIDL详解

    本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8436529.html 进程间通讯篇系列文章目录: Android查缺补漏(IP ...

  10. 浅谈location对象

    简介 Location 对象存储在 Window 对象的 Location 属性中,表示那个窗口中当前显示的文档的 Web 地址.通过Location对象,可以获取URL中的各项信息,调用对象方法也可 ...