HTML5 页面跳转和参数传递

页面跳转:

方法一:

[html]  view plain  copy
  1. <script language="javascript">  
  2.     window.location = "http://www.baidu.com";  
  3. </script>  


方法二:

[html]  view plain  copy
  1. <script language="javascript">  
  2.     document.location = "http://www.baidu.com";  
  3. </script>  


方法三:

[html]  view plain  copy
  1. <head>  
  2. <!-- 以下方式只是刷新不跳转到其他页面 -->  
  3. <meta http-equiv="refresh" content="10">  
  4. <!-- 以下方式定时转到其他页面 -->  
  5. <meta http-equiv="refresh" content="5;url=hello.html">   
  6. </head>  



html前进后退方法:

[html]  view plain  copy
  1. <input type=button value=刷新 onclick="window.location.reload()">  
  2. <input type=button value=前进 onclick="window.history.go(1)">  
  3. <input type=button value=后退 onclick="window.history.go(-1)">  
  4. <input type=button value=前进 onclick="window.history.forward()">  
  5. <input type=button value=后退 onclick="window.history.back()">  


Javascript刷新页面的几种方法:

[html]  view plain  copy
  1. 1 history.go(0)  
  2. 2 location.reload()  
  3. location=location  
  4. 4 location.assign(location)  
  5. 5 document.execCommand('Refresh')  
  6. 6 window.navigate(location)  
  7. 7 location.replace(location)  
  8. document.URL=location.href  




HTML5传递参数:

一、 使用Cookie传递参数 ,a页面保存Cookie,b页面读取,代码如下:


页面一:

[html]  view plain  copy
  1. <html>  
  2. <head>  
  3. <title>a</title>  
  4. <style type="text/css">  
  5. * {margin:0}  
  6. body {text-align:center;min-width:760px}  
  7. div {padding:3px 3px 3px 3px}  
  8. #main {width:720px;margin:0 auto;text-align:left;margin-top:30px}  
  9. #main div span {width:50px}  
  10. </style>  
  11.   
  12. <script type="text/javascript">  
  13. /***  
  14. * @param {string} cookieName Cookie名称  
  15. * @param {string} cookieValue Cookie值  
  16. * @param {number} nDays Cookie过期天数  
  17. */  
  18. function SetCookie(cookieName,cookieValue,nDays) {  
  19.     /*当前日期*/  
  20.     var today = new Date();  
  21.     /*Cookie过期时间*/  
  22.     var expire = new Date();  
  23.     /*如果未设置nDays参数或者nDays为0,取默认值1*/  
  24.     if(nDays == null || nDays == 0) nDays = 1;  
  25.     /*计算Cookie过期时间*/  
  26.     expire.setTime(today.getTime() + 3600000 * 24 * nDays);  
  27.     /*设置Cookie值*/  
  28.     document.cookie = cookieName + "=" + escape(cookieValue)  
  29.         + ";expires=" + expire.toGMTString();  
  30. }  
  31. function login() {  
  32.     var username = $("user").value;  
  33.     var password = $("pass").value;  
  34.     /*是否选中7天内无需登录*/  
  35.     var save = $("save").checked;  
  36.     if(username=="abc" && password=="abc") {  
  37.         if(save) SetCookie("username",username,7);  
  38.         else SetCookie("username",username,1);  
  39.         /*跳转到ex8.html页面*/  
  40.         document.location = "b.htm";  
  41.     } else {  
  42.         alert("用户名或密码错误!");  
  43.     }  
  44. }  
  45. function $(id) {  
  46.     return document.getElementById(id);  
  47. }  
  48. </script>  
  49. </head>  
  50. <body>  
  51.     <div id="main">  
  52.         <div><span>用户名:</span><input type="text" id="user" /></div>  
  53.         <div><span>密码:</span><input type="password" id="pass" /></div>  
  54.         <div>  
  55.             <input type="checkbox" id="save" />  
  56.             7天内无需登录  
  57.             <input type="button" onclick="login()" value="登录" />  
  58.         </div>  
  59.     </div>  
  60. </body>  
  61. </html>  


页面二:

[html]  view plain  copy
  1. <html>  
  2. <head>  
  3. <title>b</title>  
  4. <script type="text/javascript">  
  5. /***  
  6. *读取指定的Cookie值  
  7. *@param {string} cookieName Cookie名称  
  8. */  
  9. function ReadCookie(cookieName) {  
  10.     var theCookie = "" + document.cookie;  
  11.     var ind = theCookie.indexOf(cookieName);  
  12.     if(ind==-1 || cookieName=="") return "";  
  13.     var ind1 = theCookie.indexOf(';',ind);  
  14.     if(ind1==-1) ind1 = theCookie.length;  
  15.     /*读取Cookie值*/  
  16.     return unescape(theCookie.substring(ind+cookieName.length+1,ind1));  
  17. }  
  18.   
  19. function $(id) {  
  20.     return document.getElementById(id);  
  21. }  
  22.   
  23. function init() {  
  24.     var username = ReadCookie("username");  
  25.     if(username && username.length>0) {  
  26.         $("msg").innerHTML = "<h1>欢迎光临," + username + "!</h1>";  
  27.     } else {  
  28.         $("msg").innerHTML = "<a href='a.htm'>请登录</a>";  
  29.     }  
  30. }  
  31. </script>  
  32. </head>  
  33. <body onload="init()">  
  34.     <div id="msg"></div>  
  35. </body>  
  36. </html>  



location.href="index33.html?name=value";方式传递
(1)、
页面一
[html]  view plain  copy
  1. <span style="font-size:14px;"><!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.   
  9. <script LANGUAGE="JavaScript">  
  10.     function show(){  
  11.         var result = document.getElementById("name").value;  
  12.         location.href="index33.html?name="+result;  
  13.     }  
  14. </script>  
  15.   
  16. <style>  
  17.     .input7 {color: #999;width:145px;height:20px;border: 1px solid #CCCCCC; font-size:12px;background-color: #fff;}  
  18. </style>  
  19.   
  20.   
  21. <input type="text" id="name" class="input7">  
  22. <input type="button" value="OK" onclick="show()"/>  
  23.   
  24.   
  25. </body>  
  26. </html></span>  


页面二:
[html]  view plain  copy
  1. <span style="font-size:14px;"><!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.   
  9. <script>  
  10.     function getvalue(name) {  
  11.   
  12.         var str = window.location.search;   //location.search是从当前URL的?号开始的字符串  
  13.   
  14.         console.log()  
  15.   
  16.         if (str.indexOf(name) != -1) {  
  17.   
  18.             var pos_start = str.indexOf(name) + name.length + 1;  
  19.             var pos_end = str.indexOf("&", pos_start);  
  20.   
  21.             if (pos_end == -1) {  
  22.                 alert(str.substring(pos_start));  
  23.             } else {  
  24.                 alert("没有此值~~");  
  25.             }  
  26.         }  
  27.     }  
  28. </script>  
  29.   
  30. <input type="button" onclick="getvalue('name')" value="GetValue">  
  31. </body>  
  32. </html></span><span style="font-size: 14px;">  
  33. </span>  


(2)、
页面一:
[html]  view plain  copy
  1. <span style="font-size:14px;"><!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title></title>  
  6.     <script>  
  7.         function to(){  
  8.   
  9.             var  getval = document.getElementById("cc").value;  
  10.             var  aa = document.getElementById("aa").value;  
  11.             location.href = "index22.html?cc="+getval + "&" +"aa="+aa;  
  12.         }  
  13.     </script>  
  14. </head>  
  15. <body>  
  16.   
  17.   
  18. <input type="text" id="aa">  
  19. <input type="text" id ="cc" >  
  20. <input type="button"  value="a"  onclick="to()” >  
  21. </body>  
  22. </html></span>  

页面二:
[html]  view plain  copy
  1. <span style="font-size:14px;"><!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title></title>  
  6.     <script>  
  7.         var thisURL = document.URL;  
  8.   
  9.         //分割成字符串  
  10.         var  getval =thisURL.split('?')[1];  
  11.   
  12.         var keyValue = getval.split('&');  
  13.   
  14.         var showval = "所有value 值为:";  
  15.         for(var i = 0;i <keyValue.length;i++){  
  16.             var oneKeyValue = keyValue[i];  
  17.             var oneValue = oneKeyValue.split("=")[1];  
  18.   
  19.             showval = showval + oneValue + ";"  
  20.         }  
  21.   
  22.         function  showvalf(){  
  23.             alert(showval);  
  24.             document.getElementById('ee').value=showval;  
  25.         }  
  26.     </script>  
  27.   
  28. </head>  
  29. <body onload="showvalf()">  
  30.   
  31. <input type="text"  id ="ee" >  
  32. </body>  
  33. </html>  
  34. </span>  
转载来自:http://blog.csdn.net/fivenineminutes/article/details/50949656
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值