This blog finishing two ways to pass parameters from one page to another page layer layer.
A. By way of cookie
html 1. transfer cookie page, here named a.html
Please enter your user name and password:
<input id="userName" type="text" /> <input id="passwords" type="password" /> <button id="btn">设置</button> <button onclick="login()">传递cookie</button> <button onclick="deletecookie()">删除</button>
2.a.html js code
// Set Cookie var the setCookie = function (name, value, Day) { time // set equal to 0 if not set expires property, Cookie deleted when the browser is closed var expires = day * 24 * 60 * 60 * 1000 ; var = exp new new a Date (); exp.setTime (exp.getTime () + Expires); the document.cookie = name + "=" + value + "; Expires =" + exp.toUTCString (); }; // remove Cookie var delCookie = function (name) { the setCookie (name, '', -1); }; // transfer Cookie function Login () { var name = document.getElementById ( "the userName"); var = document.getElementById pass ( "Passwords"); the setCookie ( 'the userName', name.value,. 7) the setCookie ( 'password', pass.value,. 7); LOCATION.href = 'b.html' } function deletecookie() { delCookie('userName',' ',-1) }
3. Accept the cookie page, here defined as b.html
<button onclick="getcookie()">获取</button>
4. b.html js code
// Get the code cookie var the getCookie = function (name) { var ARR; var = new new REG the RegExp ( "(^ |)" + name + "= ([^;] *) (; | $)"); IF ( = document.cookie.match ARR (REG)) { return ARR [2]; } the else return null; }; // click function is called after the acquisition button function getCookie () { the console.log (the getCookie ( "the userName")) ; the console.log (the getCookie ( "password")) }
II. By way of transmission parameters url
The case is to pass parameters from a.html b.html page
1. a.html code
<input type = "text" value = " Guess Who"> <the Button onclick = "Jump ()"> Jump </ button>
2. Click the jump button can be passed to b.html input value value of the tag
function jump() { var s = document.getElementsByTagName('input')[0]; location.href='7.获取参数.html?'+'txt=' + encodeURI(s.value); }
3. b.html code
<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;
Adoption of the localStorage
By passing parameters localStorage similar cookie. But note: To access a localStorage object page must come from the same domain (subdomain invalid), using the same protocol, on the same port.
js file 1. a.html in
// page which is transmitted to the localStorage the location.href = 'b.html' // set localStorage window.localStorage.setItem ( 'User', 'haha');
2.b.html files
<button onclick = "getcookie () "> Get </ Button> function getCookie () { // Get the passed localStorage the console.log (window.localStorage.getItem ( 'User')) }
forward from: