JavaScript implements four methods of parameter transfer between HTML pages analysis of _javascript techniques

Source: Internet
Author: User

This article illustrates four ways in which JavaScript implements parameter transfer between HTML pages. Share to everyone for your reference, specific as follows:

We know that programs on the server side asp,jsp can accept the parameters from the form on the HTML page. So, can you pass the parameters to the HTML page? OK.
Principle: To obtain the parameters by the window.location.href of the separator

Method One:


 /* Function function: Get parameters from href
 *shref:http://www.cscenter.com.cn/arg.htm?arg1=d&arg2=re
 *sargname:arg1, Arg2
 *return:the value of Arg. D, re
 *
/function Getargsfromhref (Shref, Sargname)
{
 var args = Shref . Split ("?");
 var retval = "";
 if (args[0] = = shref)/* parameter is null/
 {return
   retval; * No processing/
 } 
 var str = args[1];
 args = Str.split ("&");
 for (var i = 0; i < args.length i + +)
 {
  str = args[i];
  var arg = str.split ("=");
  if (arg.length <= 1) continue;
  if (arg[0] = = sargname) retval = arg[1];
 }
 return retval;
}

Method Two:

function GetValue (name)
{
var str=window.location.search;
if (str.indexof (name)!=-1)
{
var pos_start=str.indexof (name) +name.length+1;
var pos_end=str.indexof ("&", Pos_start);
if (pos_end==-1)
{return
str.substring (Pos_start);
}
else
{return
str.substring (pos_start,pos_end)
}
} '
else {return
' does not have this name value ';
}
}
Alert (GetValue (name));

Method Three:

Request = {
querystring:function (item) {
var svalue = Location.search.match (New RegExp ("[\?\&]" + Item + "= ([^\&]*) (\&?) "," I ");
Return svalue? SVALUE[1]: svalue
}
}
Alert (Request.QueryString ("id"));

Method Four:

var Url=location.search;
var Request = new Object ();
if (Url.indexof ("?")! =-1)
{
 var str = url.substr (1);//Remove the number
 STRs = Str.tolowercase ();
 STRs = Strs.split ("&");
 for (Var i=0;i<strs.length;i++)
 {
  request[strs[i].split ("=") [0]]=unescape (Strs[i].split ("=") [1]);
 }
var mapwidth = request["W"];
var mapheight = request["H"];

I hope this article will help you with JavaScript programming.

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.