JS implementation put a page layer data lucky 28 source sold to another page

Source: Internet
Author: User
Tags setcookie

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

  1. 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)
    }
  2. A page that accepts cookies, defined here as b.html
    <button onclick= "GetCookie ()" > Get </button>
  3. 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

  4. 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);
    }
  5. 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.

  6. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.