JS實(shí)現(xiàn)把一個(gè)頁(yè)面層數(shù)據(jù)傳遞到另一個(gè)頁(yè)面的兩種方式
由于之前面試,被問(wèn)到過(guò)此問(wèn)題,所以今天特意整理了一下。由于自己技術(shù)水平有限,若存在錯(cuò)誤,歡迎提出批評(píng)。
本博客整理了兩種方式從一個(gè)頁(yè)面層向另一個(gè)頁(yè)面層傳遞參數(shù)。
一. 通過(guò)cookie方式
1. 傳遞cookie頁(yè)面的html,此處命名為a.html
請(qǐng)輸入用戶名和密碼:
<input id="userName" type="text" /> <input id="passwords" type="password" /> <button id="btn">設(shè)置</button> <button onclick="login()">傳遞cookie</button> <button onclick="deletecookie()">刪除</button>
2.a.html的js代碼
//設(shè)置cookie var setCookie = function (name, value, day) { //當(dāng)設(shè)置的時(shí)間等于0時(shí),不設(shè)置expires屬性,cookie在瀏覽器關(guān)閉后刪除 var expires = day * 24 * 60 * 60 * 1000; var exp = new Date(); exp.setTime(exp.getTime() + expires); document.cookie = name + "=" + value + ";expires=" + exp.toUTCString(); }; //刪除cookie var delCookie = function (name) { setCookie(name, ' ', -1); }; //傳遞cookie function login() { var name = document.getElementById("userName"); var pass = document.getElementById("passwords"); setCookie('userName',name.value,7) setCookie('password',pass.value,7); location.href = 'b.html' } function deletecookie() { delCookie('userName',' ',-1) }
3. 接受cookie的頁(yè)面,此處定義為b.html
<button onclick="getcookie()">獲取</button>
4. b.html的js代碼
//獲取cookie代碼 var getCookie = function (name) { var arr; var reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); if (arr = document.cookie.match(reg)){ return arr[2]; } else return null; }; //點(diǎn)擊獲取按鈕之后調(diào)用的函數(shù) function getcookie() { console.log(getCookie("userName")); console.log(getCookie("password")) }
二. 通過(guò)url傳遞參數(shù)的方式
該案例也是從a.html向b.html頁(yè)面?zhèn)鬟f參數(shù)
1. a.html的代碼
<input type="text" value="猜猜我是誰(shuí)"> <button onclick="jump()">跳轉(zhuǎn)</button>
2.點(diǎn)擊跳轉(zhuǎn)按鈕可以將input標(biāo)簽的value值傳遞到b.html
function jump() { var s = document.getElementsByTagName('input')[0]; location.href='7.獲取參數(shù).html?'+'txt=' + encodeURI(s.value); }
3. b.html中的代碼
<div id="box"></div> var loc = location.href; var n1 = loc.length; var n2 = loc.indexOf('='); var txt = decodeURI(loc.substr(n2+1,n1-n2)); var box = document.getElementById('box'); box.innerHTML = txt;
三.通過(guò)localStorage
通過(guò)localStorage傳遞參數(shù)類似cookie。但是要注意:要訪問(wèn)一個(gè)localStorage對(duì)象,頁(yè)面必須來(lái)自同一個(gè)域名(子域名無(wú)效),使用同一種協(xié)議,在同一個(gè)端口上。
1. a.html中的js文件
//將localStorage傳遞到哪個(gè)頁(yè)面 location.href = 'b.html' //設(shè)置localStorage window.localStorage.setItem('user','haha');
2.b.html中的文件
<button onclick="getcookie()">獲取</button> function getcookie() { //獲取傳遞過(guò)來(lái)的localStorage console.log(window.localStorage.getItem('user')) }
總結(jié)
以上所述是小編給大家介紹的JS實(shí)現(xiàn)把一個(gè)頁(yè)面層數(shù)據(jù)傳遞到另一個(gè)頁(yè)面的兩種方式,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
詳解Webpack抽離第三方類庫(kù)以及common解決方案
這篇文章主要介紹了詳解Webpack抽離第三方類庫(kù)以及common解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03uni-app實(shí)現(xiàn)數(shù)據(jù)下拉刷新功能實(shí)例
很多列表頁(yè)總數(shù)量很大,一次性查詢加載會(huì)導(dǎo)致頁(yè)面有很長(zhǎng)時(shí)間的空白期,自然體驗(yàn)感極差,就會(huì)使用分頁(yè)加載數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于uni-app實(shí)現(xiàn)數(shù)據(jù)下拉刷新功能實(shí)例的相關(guān)資料,需要的朋友可以參考下2022-08-08javascript function、指針及內(nèi)置對(duì)象
該文摘自于匿名教程總結(jié),希望對(duì)初學(xué)js的同學(xué)有幫助,因?yàn)樗鉀Q了我學(xué)習(xí)js的眾多迷惑。。。2009-02-02