JS implementation of the page and the value of several methods of the page between the pros and cons

Source: Internet
Author: User
Tags setcookie sessionstorage

1. Cookie value, disadvantage: Cookie storage requires server support, local direct running of static files is not possible

<script>//Add a cookiefunctionCp_add_cookie (num) {Setcookie ("Cp_keynum", num,2);}//FindfunctionCp_seek_cookie () {varCp_keynum=getcookie ("Cp_keynum");varFind_result = document.getElementById ("Find_result2"); find_result.innerhtml= "Cp_keynum = =" +Cp_keynum; }  //a function that sets the value of a cookie to create a function to store the visitor's namefunctionSetcookie (cname,cvalue,exdays) {varD =NewDate (); D.settime (D.gettime ()+ (exdays*24*60*60*1000)); varexpires = "expires=" +d.togmtstring (); Document.cookie= cname+ "=" +cvalue+ "; "+expires;}//gets the function of the cookie value, creates a function the user returns the value of the specified cookiefunctionGetCookie (CNAME) {varName = CNAME + "="; varCA = Document.cookie.split ('; '));  for(vari=0; i<ca.length; i++) {        varc =Ca[i].trim (); if(C.indexof (name) ==0)returnc.substring (name.length,c.length); }    return"";} </script>

2. Localstorage and Sessionstorage values, advantages: Local static files can support

if(typeof(Storage) = = "Undefined") {document.getElementById ("Result"). Innerhtml= "Sorry, your browser does not support Web storage. ";}//Save DatafunctionSave (cp_value) {varnum =NewObject; Num.cp_keynum= "Key_num"; Num.cp_num_value=Cp_value; varstr = json.stringify (num);//convert an object to a stringLocalstorage.setitem (NUM.CP_KEYNUM,STR); Alert ("Add Success");} //Find Datafunctionfind () {varCp_keynum = "Key_num"; varstr =Localstorage.getitem (Cp_keynum); varFind_result = document.getElementById ("Find_result"); varnum =json.parse (str); Find_result.innerhtml= Cp_keynum + "= =" +Num.cp_num_value; }  

3. URL transfer value. Advantages: Fast speed. Cookies and Localstorage and sessionstorage are both slow and unresponsive problems, I write in a page and read it on page B. Sometimes null values are read.

    functionGeturlparam (name) {//regular-expression filtering        varReg =NewRegExp ("(^|&)" + name + "= ([^&]*) (&|$)", "I"); Console.log ("location.search===" +Location.search); //substr (1): Extracts some characters from the first position of a stringConsole.log ("location.search.substr (1) = = =" +location.search.substr (1)); //match (): Retrieves the specified value that matches the regular expression within the string, returning an array to rConsole.log ("Window.location.search.substr (1). Match (reg) = = =" +window.location.search.substr (1). Match (REG)); varr = Window.location.search.substr (1). Match (REG); //gets the value of subscript 2 in the R array, (subscript starting from 0), decoded with decodeURI ()Console.log ("decodeURI (r[2]) = = =" +decodeuri (r[2])); if(r! =NULL)returndecodeURI (r[2]);return NULL;

}

JS implementation of the page and the value of several methods of the page between the pros and cons

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.