As a result of previous interviews, was asked this question lucky 28 source Sale (www.1159880099.com) QQ1159880099, so today deliberately tidied up a bit. Because of their limited technical level, if there are errors, welcome to criticize.
This blog organizes two ways to pass parameters from one page layer to another.
I. By way of a cookie
- The HTML that passes the cookie page, named a.html here
Please enter your user name and password:
<input id= "UserName" type= "text"/>
<input id= "Passwords" type= "password"/>
<button id= "btn" > Settings </button>
<button onclick= "Login ()" > Delivery cookie</button>
<button onclick= "Deletecookie ()" > Delete </button>
2.a.html's JS code
Set cookies
var Setcookie = function (name, value, day) {
When the time set is equal to 0 o'clock, the Expires property is not set and the cookie is deleted after the browser is closed
var expires = Day -60 -1000;
var exp = new Date ();
Exp.settime (Exp.gettime () + expires);
Document.cookie = name + "=" + Value + "; expires=" + exp.toutcstring ();
};
Delete Cookies
var Delcookie = function (name) {
Setcookie (name, ' ',-1);
};
Pass a 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)
}
- A page that accepts cookies, defined here as b.html
<button onclick= "GetCookie ()" > Get </button>
B.html's JS code
Get Cookie Code
var GetCookie = function (name) {
var arr;
var reg = new RegExp ("(^|)" + name + "= ([^;] *)(;|$)");
if (arr = Document.cookie.match (reg)) {
return arr[2];
}
Else
return null;
};
function called after clicking get button
function GetCookie () {
Console.log (GetCookie ("UserName"));
Console.log (GetCookie ("password"))
}
Two. How parameters are passed by URL
This case also passes parameters from a.html to b.html page
- A.html's Code
<input type= "text" value= "Guess Who I Am" >
<button onclick= "Jump ()" > Jumps </button>
2. Click the Jump button to pass the value of the input tag to the b.html
Function jump () {
var s = document.getelementsbytagname (' input ') [0];
Location.href= ' 7. Get parameters. html? ' + ' txt= ' + encodeURI (s.value);
}
Code in the 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;
Three. by Localstorage
Pass parameters like cookies through Localstorage. But note: To access a Localstorage object, the page must be from the same domain name (invalid subdomain), using the same protocol, on the same port.
- JS file in a.html
Which page to pass the Localstorage to
Location.href = ' b.html '
Set Localstorage
Window.localStorage.setItem (' user ', ' haha ');
Files in 2.b.html
<button onclick= "GetCookie ()" > Get </button>
function GetCookie () {
Get the localstorage passed over
Console.log (Window.localStorage.getItem (' user '))
}
JS implementation put a page layer data lucky 28 source sold to another page