js 頁面之間的跳轉、傳參以及返回上一頁


js實現html 頁面之間的跳轉傳參以及返回上一頁的相關知識點

一、頁面之間的跳轉傳參

1、在頁面之間跳轉的方式有兩種:

window.location.href=”test.html?num=10”   地址會改變參數也會被傳遞但是不會打開新窗口

window.open("test.html") 這樣會重新打開一個新窗口。

2、獲取參數

如果是按照第一種方式進行了傳遞則有參數,那么我們怎們獲取url中的參數那,那就使用js默認的屬性:  var url = location.search; 

其中的location.search 就是js自動獲取url中? 后的所有值。獲取了這個之后就可以使用substring,split等來獲取參數了。

3、實例展示

[javascript] view plain copy
print ?
  1.         // 跳轉url 以及傳遞的參數  
  2.     window.location.href='http://img.as.com/news/image/newscenter/20111107zt/whd/30share/jieguo1n.html?money='+nums+'&url='+fxurl;  
  3.   
  4.         // 獲取money,以及分型的地址  
  5.     function GetRequest() {  
  6.           var url = location.search;   
  7.          var theRequest = new Object();  
  8.           if (url.indexOf("?") != -1) {  
  9.             var str = url.substr(1);  
  10.             //alert(str);  
  11.             var strs= new Array();     
  12.              strs = str.split('&');  
  13.             var money=strs[0].substring(6);  
  14.             fxurl=(strs[1].substring(4)).trim();  
  15.             //alert(fxurl);  
  16.             var  view=money+"元";  
  17.             $("#jieguo1m").html(view);  
  18.       }  
  19. }  
  20. GetRequest();  


這樣當跳轉到url指定的頁面后,調用GetRequest();這個函數,函數中的location.search;來獲取了url中?后的所有參數,接下來就是按照需求來解析了。


二、返回上一頁

1、在原來的窗體中直接跳轉用

[javascript] view plain copy
print ?
  1. window.location.href="test.html";  
2、 返回上一頁原頁面中的表單中的數據會丟失

[javascript] view plain copy
print ?
  1. window.history.go(-1);  
3、 返回上一頁原頁面 表單中的內容會保留

[javascript] view plain copy
print ?
  1. window.history.back();  

實例:

1、

[javascript] view plain copy
print ?
  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()">  

2、

[javascript] view plain copy
print ?
  1. <a href="javascript:history.go(-1)">返回上一頁</a>   
  2. <a href="javascript:location.reload()">刷新當前頁面</a>   
  3. <a href="javascript:" onclick="history.go(-2); ">返回前兩頁</a>   
  4. <a href="javascript:" onclick="self.location=document.referrer;">返回上一頁並刷新</a>   
  5. <a href="javascript:" onclick="history.back(); ">返回上一頁</a>   

這里看到了 <a href="javascript:">就說說這個:

[javascript] view plain copy
print ?
  1. <a href=”javascript:” onclick=”fun1()” >  </a>  
  2. <a href=”javascript: undefined” onclick=”fun1()” >  </a>  
  3. <a href=”javascript:void(0)” onclick=”fun1()” >  </a>  
  4. 這三種方式,要實現的效果是一樣的。即不執行跳轉而是執行對應的函數,而JavaScript:void(0)在頁面內容很多的時候會好一些  
  5. 而且W3C標准不推薦在href里面執行javascript語句,所以還是用 onclick事件觸發吧,所以我們不要這樣寫:<a href=javascript:function()>  </a>  





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM