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. sql server 里的文件和文件组使用

    转自:https://www.cnblogs.com/woodytu/p/5821827.html 参考:https://www.sqlskills.com/blogs/paul/files-and- ...

  2. 202-React.Component组件、生命周期

    一.概述 React可以将组件定义为类或函数.定义为类的组件当前提供了更多的功能.要定义React组件类,您需要扩展React.Component: class Welcome extends Rea ...

  3. MySQL创建索引命令

    MySQL索引类型 普通索引 创建索引的方式 -- 直接新建索引 CREATE INDEX indexName ON mytable(username(length)) -- 修改表结构新建索引 AL ...

  4. UVA大模拟代码(白书训练计划1)UVA 401,10010,10361,537,409,10878,10815,644,10115,424,10106,465,10494

    白书一:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=64609#overview 注意UVA没有PE之类的,如果PE了显示WA. UVA ...

  5. 利用Flash XMLSocket实现”服务器推”技术

    利用Flash XML Socket实现”服务器推”技术的基础是:1.Flash提供了XMLSocket类,服务器利用Socket向Flash发送数据:2.JavaScript和Flash的紧密结合, ...

  6. docker——三剑客之Docker swarm

    Docker Swarm是Docker官方的三剑客项目之一,提供Docker容器集群服务,是Docker官方对容器云生态进行支持的核心方案.使用它,用户可以将多个Docker主机封装为单个大型的虚拟D ...

  7. 3.1 Templates -- Handlerbars Basics(Handlerbars基础知识)

    一.简介 Ember.js使用Handlerbars模板库来强化应用程序的用户界面.它就像普通的HTML,但也给你嵌入表达式去改变现实的内容. Ember使用Handlerbars并且用许多新特性去扩 ...

  8. VS2010/MFC编程入门之二十(常用控件:静态文本框)

    上一节鸡啄米讲了颜色对话框之后,关于对话框的使用和各种通用对话框的介绍就到此为止了.从本节开始鸡啄米将讲解各种常用控件的用法.常用控件主要包括:静态文本框.编辑框.单选按钮.复选框.分组框.列表框.组 ...

  9. uva1424

    Traveling salesmen of nhn. (the prestigious Korean internet company) report their current location t ...

  10. StarUML激活

    感谢  http://blog.csdn.net/mergades/article/details/46662413 1,打开对应 mac版本的安装包位置,在对应目录/Applications/Sta ...