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、實例展示
- // 跳轉url 以及傳遞的參數
- window.location.href='http://img.as.com/news/image/newscenter/20111107zt/whd/30share/jieguo1n.html?money='+nums+'&url='+fxurl;
- // 獲取money,以及分型的地址
- function GetRequest() {
- var url = location.search;
- var theRequest = new Object();
- if (url.indexOf("?") != -1) {
- var str = url.substr(1);
- //alert(str);
- var strs= new Array();
- strs = str.split('&');
- var money=strs[0].substring(6);
- fxurl=(strs[1].substring(4)).trim();
- //alert(fxurl);
- var view=money+"元";
- $("#jieguo1m").html(view);
- }
- }
- GetRequest();
這樣當跳轉到url指定的頁面后,調用GetRequest();這個函數,函數中的location.search;來獲取了url中?后的所有參數,接下來就是按照需求來解析了。
二、返回上一頁
1、在原來的窗體中直接跳轉用
- window.location.href="test.html";
- window.history.go(-1);
- window.history.back();
實例:
1、
- <input type=button value=刷新 onclick="window.location.reload()">
- <input type=button value=前進 onclick="window.history.go(1)">
- <input type=button value=后退 onclick="window.history.go(-1)">
- <input type=button value=前進 onclick="window.history.forward()">
- <input type=button value=后退 onclick="window.history.back()">
2、
- <a href="javascript:history.go(-1)">返回上一頁</a>
- <a href="javascript:location.reload()">刷新當前頁面</a>
- <a href="javascript:" onclick="history.go(-2); ">返回前兩頁</a>
- <a href="javascript:" onclick="self.location=document.referrer;">返回上一頁並刷新</a>
- <a href="javascript:" onclick="history.back(); ">返回上一頁</a>
這里看到了 <a href="javascript:">就說說這個:
- <a href=”javascript:” onclick=”fun1()” > </a>
- <a href=”javascript: undefined” onclick=”fun1()” > </a>
- <a href=”javascript:void(0)” onclick=”fun1()” > </a>
- 這三種方式,要實現的效果是一樣的。即不執行跳轉而是執行對應的函數,而JavaScript:void(0)在頁面內容很多的時候會好一些
- 而且W3C標准不推薦在href里面執行javascript語句,所以還是用 onclick事件觸發吧,所以我們不要這樣寫:<a href=javascript:function()> </a>
