码迷,mamicode.com
首页 > Web开发 > 详细

URL跳转的几种方式

时间:2016-08-09 22:15:26      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:

1、HTML:

①、

<head> 
<!-- 以下方式只是刷新不跳转到其他页面 -->
<meta http-equiv="refresh" content="10"> 
<!-- 以下方式定时转到其他页面 -->
<meta http-equiv="refresh" content="5;url=hello.html">  
</head> 

2、javascript:

①、window.location.href方式

<script language="javascript" type="text/javascript">  
// 以下方式直接跳转 
window.location.href=hello.html; 
// 以下方式定时跳转 
setTimeout("javascript:location.href=‘hello.html‘", 5000);  
</script> 

②、window.navigate方式跳转

<script language="javascript">
    window.navigate("target.aspx");
</script>

③、window.loction.replace方式

<script language="javascript">
    window.location.replace("target.aspx");
</script>

④、self.location方式

<script language="JavaScript">
          self.location=target.aspx;
   </script>

⑤、top.location方式

<script language="javascript">
          top.location=target.aspx;
   </script>

⑥、返回方式

<script language="javascript">
    alert("返回");
    window.history.back(-1);
   </script>

PS:Javascript刷新页面的几种方法: 
 history.go(0) 
 location.reload() 
 location=location 
 location.assign(location) 
 document.execCommand(‘Refresh‘) 
 window.navigate(location) 
 location.replace(location) 
 document.URL=location.href 

3、Java类(servlet):

①、response.sendRedirect("/a.jsp");

  页面的路径是相对路径。sendRedirect可以将页面跳转到任何页面,不一定局限于本web应用中,如:

response.sendRedirect("http://www.jb51.net");

  跳转后浏览器地址栏变化。

  这种方式要传值出去的话,只能在url中带parameter或者放在session中,无法使用request.setAttribute来传递。

②、RequestDispatcher dispatcher = request.getRequestDispatcher("/a.jsp");

  dispatcher .forward(request, response);

  页面的路径是相对路径。forward方式只能跳转到本web应用中的页面上。

  跳转后浏览器地址栏不会变化。

  跳转到同级目录下的页面。

  使用这种方式跳转,传值可以使用三种方法:url中带parameter,session,request.setAttribute

4、JSP:

①、response.sendRedirect();

  同上。

②、response.setHeader("Location","");

  此语句前不允许有out.flush(),如果有,页面不会跳转。

  跳转后浏览器地址栏变化

  此语句后面的语句执行完成后才会跳转

URL跳转的几种方式

标签:

原文地址:http://www.cnblogs.com/liaozs/p/5754702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!