Three methods for passing javascript Static Page values

Source: Internet
Author: User

 

I. URL of JavaScript Static Page value transfer

Pass through the URL to pass the value. Connect the information to be passed to the URL.

Post.htm

 

 

<Input type = "text" name = "username">

<Input type = "text" name = "sex">

<Input type = "button" value = "Post">

<Script language = "javascript">

Function Post ()

{

// Single value read.htm? Username = baobao;

// What is the full value of read.htm? Username = baobao & sex = male;

Url = "Read.htm? Username = "+ escape (document. all. username. value );

Url + = "& sex =" + escape (document. all. sex. value );

Location. href = url;

}

</Script

Read.htm

 

 

<Script language = "javascript">

/*

* --------------- Read.htm -----------------

* Request [key]

* Function: Implement ASP to obtain the URL string, Request ("AAA ")

* Parameter: key, String.

* Instance: alert (Request ["AAA"])

* --------------- Request.htm -----------------

*/

Var url = location. search;

Var Request = new Object ();

If (url. indexOf ("? ")! =-1)

{

Var str = url. substr (1) // remove? No.

Strs = str. split ("&");

For (var I = 0; I <strs. length; I ++)

{

Request [strs [I]. split ("=") [0] = unescape (strs [I]. split ("=") [1]);

}

}

Alert (Request ["username"])

Alert (Request ["sex"])

</Script> <script language = "JavaScript">

<! --

Function Request (strName)

{

Var strHref = "www.abc.com/index.htm? A = 1 & B = 1 & c = test ";

Var intPos = strHref. indexOf ("? ");

Var strRight = strHref. substr (intPos + 1 );

Var arrTmp = strRight. split ("&");

For (var I = 0; I <arrTmp. length; I ++)

{

Var arrTemp = arrTmp [I]. split ("= ");

If (arrTemp [0]. toUpperCase () = strName. toUpperCase () return arrTemp [1];

}

Return "";

}

Alert (Request (""));

Alert (Request ("B "));

Alert (Request ("c "));

// -->

</Script>

<Script>

String. prototype. getQuery = function (name)

{

Var reg = new RegExp ("(^ | &)" + name + "= ([^ &] *) (& | $ )");

Var r = this. substr (this. indexOf ("? ") + 1). match (reg );

If (r! = Null) return unescape (r [2]); return null;

}

Var str = "www.abc.com/index.htm? A = 1 & B = 1 & c = test ";

Alert (str. getQuery (""));

Alert (str. getQuery ("B "));

Alert (str. getQuery ("c "));

</Script

Advantage: The value is convenient. Cross-origin is supported.

Disadvantage: The value length is limited.

 

Ii. Cookie for passing JavaScript Static Page values

Cookie is used by the browser to store a small amount of named data.

 

It is associated with a specific webpage or website.

 

Cookies are used to provide memory for browsers,

 

So that scripts and server programs can use the input data of another page on one page.

 

Post.htm

 

 

<Input type = "text" name = "txt1">

<Input type = "button" value = "Post">

<Script language = "javascript">

Function setCookie (name, value)

{

/*

* --------------- SetCookie (name, value )-----------------

* SetCookie (name, value)

* Function: set the value of the variable name.

* Parameter: name, string; value, String.

* Instance: setCookie ('username', 'baobao ')

* --------------- SetCookie (name, value )-----------------

*/

Var Days = 30; // This cookie will be saved for 30 Days

Var exp = new Date ();

Exp. setTime (exp. getTime () + Days x 24x60*60*1000 );

Document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();

Location. href = "Read.htm"; // receiving page.

}

</Script

 

Read.htm

 

 

 

<Script language = "javascript">

Function getCookie (name)

{

/*

* --------------- GetCookie (name )-----------------

* GetCookie (name)

* Function: Get the variable name value.

* Parameter: name, String.

* Instance: alert (getCookie ("baobao "));

* --------------- GetCookie (name )-----------------

*/

Var arr = document. cookie. match (new RegExp ("(^ |)" + name + "= ([^;] *) (; | $ )"));

If (arr! = Null) return unescape (arr [2]); return null;

}

Alert (getCookie ("baobao "));

</Script

Advantage: it can be accessed from any webpage in the same source. The lifecycle can be set.

Disadvantage: The value length is limited.

 

3. Window. open for passing JavaScript Static Page values

There is a relationship between the two windows. Parent window parent.htmopen window son.htm

 

The child window can point to the parent window through window. opener. In this way, the object of the parent window can be accessed.

 

Post.htm

 

 

<Input type = text name = maintext>

<Input type = button value = "Open">

Read.htm

<Script language = "javascript">

// Window. open window.

// Use opener to point to the parent window.

Var parentText = too many opener.doc ument. all. maintext. value;

Alert (parentText );

</Script

 

Advantage: convenient value. As long as window. opener points to the parent window, all objects can be accessed. Not only can the value be accessed, but also the method of accessing the parent window. The value length is not limited.

Disadvantage: there must be a relationship between the two windows. It is the window opened by window. open. Cross-Domain is not allowed.

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.