Home Web Front-end JS Tutorial Example analysis of four methods of parameter transfer between html pages using javascript_javascript skills

Example analysis of four methods of parameter transfer between html pages using javascript_javascript skills

May 16, 2016 pm 03:25 PM
html page javascript Parameter passing

The examples in this article describe four methods of using JavaScript to transfer parameters between HTML pages. Share it with everyone for your reference, the details are as follows:

We know that on the server side asp, jsp and other programs can accept parameters from the form on the html page. So, can I pass parameters to the html page? Can.
Principle: Obtain each parameter through the separator in window.location.href

Method 1:

/*
 *函数功能:从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) /*参数为空*/
 {
   return retval; /*无需做任何处理*/
 } 
 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;
}

Copy after login

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 "没有这个name值";
}
}
alert(getvalue(name));

Copy after login

Method 3:

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

Copy after login

Method 4:

var url=location.search;
var Request = new Object();
if(url.indexOf("&#63;")!=-1)
{
 var str = url.substr(1); //去掉&#63;号
 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"];

Copy after login

I hope this article will be helpful to everyone in JavaScript programming.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Best practices for optimizing Golang function parameter passing performance Best practices for optimizing Golang function parameter passing performance Apr 13, 2024 am 11:15 AM

In order to optimize Go function parameter passing performance, best practices include: using value types to avoid copying small value types; using pointers to pass large value types (structures); using value types to pass slices; and using interfaces to pass polymorphic types. In practice, when passing large JSON strings, passing the data parameter pointer can significantly improve deserialization performance.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

JavaScript and WebSocket: Building an efficient real-time search engine JavaScript and WebSocket: Building an efficient real-time search engine Dec 17, 2023 pm 10:13 PM

JavaScript and WebSocket: Building an efficient real-time search engine Introduction: With the development of the Internet, users have higher and higher requirements for real-time search engines. When searching with traditional search engines, users need to click the search button to get results. This method cannot meet users' needs for real-time search results. Therefore, using JavaScript and WebSocket technology to implement real-time search engines has become a hot topic. This article will introduce in detail the use of JavaScript

Research on parameter passing methods in Go language Research on parameter passing methods in Go language Apr 03, 2024 pm 02:48 PM

In the Go language, there are two main ways to pass function parameters: value passing: passing a copy of the variable will not affect the original variable in the calling code. Pointer passing: Passing the address of a variable allows the function to directly modify the original variable in the calling code.

What is the parameter passing method for PHP functions? What is the parameter passing method for PHP functions? Apr 10, 2024 am 11:06 AM

There are two ways to pass parameters in PHP: call by value (the parameter is passed as a copy of the value, modification within the function does not affect the original variable) and passing by reference (the address of the parameter is passed, modification within the function will affect the original variable), when the original variable needs to be modified Use reference passing when calculating the shopping cart total price, which requires reference passing to calculate correctly.

See all articles