Three ways to share javascript static page pass values

  • 2020-03-29 23:41:34
  • OfStack

A: JavaScript static page value to pass the URL
Can pass the value through the URL. To pass the information 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()
{
//A single value Read. HTM? The username = baobao;
//More full value Read. HTM? The 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 the HTM


<script language="javascript" >

var url=location.search;
var Request = new Object();
if(url.indexOf("?")!=-1)
{
var str = url.substr(1) //Get rid of? 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.jb51.net/index.htm?a=1&b=1&c= Test 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("a"));
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.jb51.net/index.htm?a=1&b=1&c= Test test ";
alert(str.getQuery("a"));
alert(str.getQuery("b"));
alert(str.getQuery("c"));
</script>

Advantage: value is convenient. Can cross - domain.
Disadvantages: value length is limited

2: JavaScript static page value transfer of the Cookie
A Cookie is a browser that stores a small amount of named data.
It is associated with a particular web page or website.
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


<input type="text" name="txt1">
<input type="button" value="Post">
<script language="javascript" >
function setCookie(name,value)
{

var Days = 30; //This cookie will be stored for 30 days
  var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
location.href = "Read.htm"; //Receive the page.
}
</script>

Read the HTM


<script language="javascript" >
function getCookie(name)
{

var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr !=null) return unescape(arr[2]); return null;
}
alert(getCookie("baobao"));
</script>

Advantage: can be accessed in any web page within the same origin. Lifetime can be set.
Disadvantages: value length is limited.

3: JavaScript static page value transfer window.open
The parent window parent.htm opens the child window son.htm
The child window can point to the parent window by window.opener. This gives access to the parent window's objects.
Post. HTM


<input type=text name=maintext>
<input type=button value="Open">
Read.htm
<script language="javascript" >
//Window.open.
//Use the opener to point to the parent window.
var parentText = window.opener.document.all.maintext.value;
alert(parentText);
</script>

Advantage: easy to set. You can access all objects as long as window.opener points to the parent window. You can access not only the values, but also the methods of the parent window. The value length is unlimited.
Disadvantage: there should be a relationship between two Windows. That is, the window opened with window.open. Cannot cross fields.


Related articles: