Javascript: four methods for passing parameters between html pages. Example Analysis _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces four methods for passing parameters between html pages by using javascript, and analyzes common techniques for passing parameters between pages by using examples in detail, for more information about how to use javascript to transfer parameters between html pages, see the following example. We will share this with you for your reference. The details are as follows:

We know that on the server side, asp, jsp, and other programs can accept parameters sent from form on the html page. So, can we pass parameters to the html page. Yes.
Principle: parameters are obtained using the delimiter in window. location. href.

Method 1:

/** Function: Get the parameter * sHref: http://www.cscenter.com.cn/arg.htm? From href? 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 blank */{return retval; /* No processing required */} 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 2:

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 "without this name value" ;}} alert (getvalue (name ));

Method 3:

Request = {QueryString : function(item){var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)","i"));return svalue ? svalue[1] : svalue;}}alert(Request.QueryString("id"));

Method 4:

Var url = location. search; var Request = new Object (); if (url. indexOf ("? ")! =-1) {var str = url. substr (1); // remove? Strs = str. toLowerCase (); strs = strs. split ("&"); for (var I = 0; I
 
  

I hope this article will help you design JavaScript programs.

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.