Four ways to pass the value of JavaScript HTML static page

Source: Internet
Author: User
Tags setcookie

One: JavaScript static page value pass the URL Chapter
Can pass the URL to the value. Connect the information you want to pass to the URL.
Post.htm

1 The code is as follows:2 3<input type= "text" name= "username" >4<input type= "text" name= "Sex" >5<input type= "button" value= "Post" >6<script language= "JavaScript" >7 functionPost ()8 {9 //single value Read.htm?username=baobao;Ten //multiple full value read.htm?username=baobao&sex=male; Oneurl = "Read.htm?username=" +Escape (document.all.username.value); AURL + = "&sex=" +Escape (document.all.sex.value); -location.href=URL; - } the</script>


Read.htm

The code is as follows:
1<script language= "JavaScript" >2 /*3 *---------------read.htm-----------------4 * Request[key]5 * Function: Implement ASP Get URL string, Request ("AAA")6 * Parameters: Key, String.7 * Example: Alert (request["AAA"])8 *---------------request.htm-----------------9 */Ten varUrl=Location.search; One varRequest =NewObject (); A if(Url.indexof ("?")! =-1) - { -     varstr = URL.SUBSTR (1)//remove the number. theSTRs = Str.split ("&"); -      for(vari=0;i<strs.length;i++) -     { -Request[strs[i].split ("=") [0]]=unescape (strs[i].split ("=") [1]); +     } - } +Alert (request["username"]) AAlert (request["Sex"]) at</script><script language= "JavaScript" > -<!-- - functionRequest (strName) - { -     varStrhref = "www.jb51.net/index.htm?a=1&b=1&c= test test"; -     varIntpos = Strhref.indexof ("?")); in     varStrright = strhref.substr (intpos + 1); -     varArrtmp = Strright.split ("&"); to      for(vari = 0; i < arrtmp.length; i++) +     { -       vararrtemp = Arrtmp[i].split ("="); the       if(arrtemp[0].touppercase () = = Strname.touppercase ())returnArrtemp[1]; *     } $     return"";Panax Notoginseng } -Alert (Request ("a")); theAlert (Request ("B")); +Alert (Request ("C")); A // - the</script> +<script> -String.prototype.getQuery =function(name) $ { $     varReg =NewRegExp ("(^|&)" + name + "= ([^&]*) (&|$)"); -     varR = This. substr ( This. IndexOf ("?") +1). Match (reg); -     if(r!=NULL)returnUnescape (r[2]);return NULL; the } -     varstr = "www.jb51.net/index.htm?a=1&b=1&c= test test";WuyiAlert (Str.getquery ("a")); theAlert (Str.getquery ("B")); -Alert (Str.getquery ("C")); Wu</script>

Advantages: It is convenient to take the value. You can cross domains.
Disadvantage: There is a limit to the value length

Second: JavaScript static page value pass the cookie
A cookie is a browser that stores a small amount of named data.
It is associated with a particular Web page or site.
Cookies are used to provide memory to the browser,
So that scripts and server programs can use the input data of another page in one page.
Post.htm

The code is as follows:
1<input type= "text" name= "Txt1" >2<input type= "button" value= "Post" >3<script language= "JavaScript" >4 functionSetcookie (Name,value)5 {6 /*7 *---------------Setcookie (name,value)-----------------8 * Setcookie (name,value)9 * Function: Set the value of the variable nameTen * Parameters: Name, string, value, String. One * Example: Setcookie (' username ', ' Baobao ') A *---------------Setcookie (name,value)----------------- - */ -     vardays = 30;//This cookie will be saved for 30 days the     varExp =NewDate (); -Location.href = "read.htm";//receive the page. - } -</script>

Read.htm

The code is as follows:
1<script language= "JavaScript" >2 functionGetCookie (name)3 {4 /*5 *---------------GetCookie (name)-----------------6 * GetCookie (name)7 * Function: Gets the value of the variable name8 * Parameters: Name, String.9 * Example: Alert (GetCookie ("Baobao"));Ten *---------------GetCookie (name)----------------- One */ A     vararr = Document.cookie.match (NewRegExp ("(^|)" +name+ "= ([^;] *)(;|$)")); -     if(Arr! =NULL)returnUnescape (arr[2]);return NULL; - } theAlert (GetCookie ("Baobao")); -</script>

Advantage: can be accessed within any Web page within the same origin. Life can be set.
Disadvantage: There is a limit to the value length.

Three: window.open of JavaScript static page value passing
There is a relationship between the two windows. Parent Window parent.htm Open child window son.htm
The child window can point to the parent window through Window.opener. This allows access to the parent window's object.
Post.htm

The code is as follows:
1 <input type=text name=maintext>2 <input type=button value= "Open" >3  Read.htm4 <script language= "JavaScript" >5//window.open open window. 6 // use opener to point to the parent window. 7 var parenttext = window.opener.document.all.maintext.value; 8 alert (parenttext); 9 </script>      

Advantages: The value is convenient. As long as Window.opener points to the parent window, you can access all objects. You can access not only the value, but also the method of the parent window. The value length is unlimited.
Cons: Two window to have a relationship. is a window opened with window.open. Cannot cross domain.

Four: HTML local store transfer value Chapter
There is a relationship between the two windows. Parent Window parent.htm Open child window son.htm
The child window can point to the parent window through Window.opener. This allows access to the parent window's object.
Post.htm

The code is as follows:
1<! DOCTYPE html>234<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>5<title></title>6<script>7         functionset () {8             //because it is a new technology, you can check whether your browser supports it with the following code9             if(window.localstorage) {Ten                 //value of the stored variable OneLocalstorage.name =Document.all.username.value; ALocation.href = ' read.html '; -}Else { -Alert ("Not support"); the             } -         } -</script> - +<body> -<input type= "text" name= "username"/> +<input type= "button" name= "" value= "POST" onclick= "set ()"/> A</body> at

Read.html

1<! DOCTYPE html>234<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>5<title></title>6<script>7         varValue = localstorage["Name"];8 alert (value);9</script>Ten One<body> A  -</body> -

Pros: The best solution .

Disadvantage: The browser may not be supported.

Four ways to pass the value of JavaScript HTML static page

Related Article

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.