Table of Contents
1. Through cookie method
1. Pass the html of the cookie page, here named a.html
2.a.html js code
3. The page that accepts cookies, defined here as b.html
4. The js code of b.html
2. The way to pass parameters through the url
1. The code of a.html
2. Click the jump button to change the value of the input tag The value is passed to b.html
3. Code in b.html
3. Passing parameters through localStorage
1. js files in a.html
2. Files in b.html
Home Web Front-end JS Tutorial js code to implement data transfer between pages

js code to implement data transfer between pages

Aug 13, 2018 pm 03:41 PM
data transfer

The content of this article is about the js implementation code for data transmission on the page. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Since I have been asked this question in previous interviews, I have sorted it out today. Due to my limited technical level, if there are any mistakes, you are welcome to criticize.

This blog summarizes two ways to pass parameters from one page layer to another.

1. Through cookie method

请输入用户名和密码:
<input id="userName" type="text" />
<input id="passwords" type="password" />
<button id="btn">设置</button>
<button onclick="login()">传递cookie</button>
<button onclick="deletecookie()">删除</button>
Copy after login

2.a.html js code

//设置cookie
var setCookie = function (name, value, day) {
    //当设置的时间等于0时,不设置expires属性,cookie在浏览器关闭后删除
    var expires = day * 24 * 60 * 60 * 1000;
    var exp = new Date();
    exp.setTime(exp.getTime() + expires);
    document.cookie = name + "=" + value + ";expires=" + exp.toUTCString();
};
//删除cookie
var delCookie = function (name) {
    setCookie(name, &#39; &#39;, -1);
};
//传递cookie
function login() {
    var name = document.getElementById("userName");
    var pass = document.getElementById("passwords");
    setCookie(&#39;userName&#39;,name.value,7)
    setCookie(&#39;password&#39;,pass.value,7);
    location.href = &#39;b.html&#39;
}
function deletecookie() {
    delCookie(&#39;userName&#39;,&#39; &#39;,-1)
}
Copy after login

3. The page that accepts cookies, defined here as b.html

<button onclick="getcookie()">获取</button>
Copy after login

4. The js code of b.html

//获取cookie代码
var getCookie = function (name) {
    var arr;
    var reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg)){
        return arr[2];
    }
    else
        return null;
};
//点击获取按钮之后调用的函数
function getcookie() {
    console.log(getCookie("userName"));
    console.log(getCookie("password"))
}
Copy after login

2. The way to pass parameters through the url

This case also passes parameters from a.html to b.html page

1. The code of a.html

<input type="text" value="猜猜我是谁">
<button onclick="jump()">跳转</button>
Copy after login

2. Click the jump button to change the value of the input tag The value is passed to b.html

function jump() {
    var s = document.getElementsByTagName(&#39;input&#39;)[0];
    location.href=&#39;7.获取参数.html?&#39;+&#39;txt=&#39; + encodeURI(s.value);
}
Copy after login

3. Code in b.html

<p id="box"></p>
Copy after login
var loc = location.href;
var n1 = loc.length;
var n2 = loc.indexOf(&#39;=&#39;);
var txt = decodeURI(loc.substr(n2+1,n1-n2));
var box = document.getElementById(&#39;box&#39;);
box.innerHTML = txt;
Copy after login

3. Passing parameters through localStorage

is similar to cookies. But be careful: to access a localStorage object, the page must come from the same domain name (subdomain names are invalid), use the same protocol, and be on the same port.

1. js files in a.html

//将localStorage传递到哪个页面
location.href = &#39;b.html&#39;
//设置localStorage
window.localStorage.setItem(&#39;user&#39;,&#39;haha&#39;);
Copy after login

2. Files in b.html

<button onclick="getcookie()">获取</button>
function getcookie() {
    //获取传递过来的localStorage
    console.log(window.localStorage.getItem('user'))
}
Copy after login

Related recommendations:

jQuery What are the methods in jQuery? Commonly used methods in jQuery (with code)

Detailed introduction to the usage of data objects in js (with code)

The above is the detailed content of js code to implement data transfer between pages. For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Use provide and inject in Vue to implement data transfer and performance optimization between components Use provide and inject in Vue to implement data transfer and performance optimization between components Jul 17, 2023 pm 07:19 PM

Use provide and inject in Vue to achieve data transfer and performance optimization between components. In Vue, data transfer between components is a very common requirement. Sometimes we want to provide data at a node in the component tree and then use the data in its descendant components. In this case, we can use Vue's provide and inject to achieve this. In addition to data transfer, provide and inject can also be used for performance optimization, reducing the level of props transfer and improving component performance. prov

How to use PHP language to call API interface to realize data transfer and synchronization between systems? How to use PHP language to call API interface to realize data transfer and synchronization between systems? Sep 05, 2023 am 11:26 AM

How to use PHP language to call API interface to realize data transfer and synchronization between systems? When developing and designing modern systems, we often need to transfer and synchronize data between different systems. A common method is to use API interfaces to implement communication between systems. This article will introduce how to use PHP language to call API interface to achieve data transfer and synchronization between systems. API (Application Programming Interface) is a programmatic way to implement different systems

How to implement communication and data transfer between parent and child components in Vue components How to implement communication and data transfer between parent and child components in Vue components Oct 08, 2023 pm 09:51 PM

Vue is a popular front-end development framework that provides many convenient functions and mechanisms to help us build reusable and efficient componentized applications. In Vue, parent-child component communication and data transfer are one of the common requirements. This article will introduce in detail how to implement communication and data transfer between parent and child components in Vue, and provide specific code examples. In Vue, communication between parent and child components can be achieved through props and $emit methods. Props is the mechanism for parent components to pass data to child components, and $emi

Vue component communication: use v-bind directive for data transfer Vue component communication: use v-bind directive for data transfer Jul 07, 2023 pm 04:46 PM

Vue component communication: Use the v-bind instruction for data transfer. Vue.js is a popular front-end framework that provides powerful component development capabilities. In Vue applications, component communication is an important issue. The v-bind instruction is a data transfer method provided by the Vue framework. This article will introduce how to use the v-bind instruction to transfer data between components. In Vue, component communication can be divided into two situations: parent-child component communication and sibling component communication. Below we will introduce from these two aspects respectively:

How to use the data inside the component and the props of the component parameters to achieve data transfer in Vue How to use the data inside the component and the props of the component parameters to achieve data transfer in Vue Jun 11, 2023 pm 02:03 PM

Vue is a very popular front-end development framework, in which components are very important building blocks. In Vue, the data inside the component and the props of the component parameters can be used well together to achieve data transfer. In Vue components, data is the state data inside the component. It can be accessed and modified by all methods in the component. To use data inside a component, you can declare it in the following way: exportdefault{data(){

Front-end and back-end data transfer problems encountered in Vue development Front-end and back-end data transfer problems encountered in Vue development Oct 08, 2023 pm 01:25 PM

Front-end and back-end data transfer problems encountered in Vue development require specific code examples. With the development of front-end technology, Vue is a popular front-end framework. More and more developers choose to use Vue to develop web applications. In the Vue development process, the transmission of front-end and back-end data is a very important link. This article will introduce some common front-end and back-end data transfer problems in Vue development, and provide specific code examples to solve these problems. The front-end and back-end data transfer formats are not unified. During the front-end and back-end data transfer process,

How to use v-bind directive to pass data in Vue How to use v-bind directive to pass data in Vue Jun 11, 2023 am 10:45 AM

Vue is a popular JavaScript framework that uses many directives to make front-end development easier and more flexible. Among them, the v-bind instruction is a very important instruction in Vue, which allows us to dynamically bind data to html elements. The syntax of the v-bind directive is very simple and can be used on any html tag, for example: &lt;imgv-bind:src="imageSrc"&gt; In this example, v-bind refers to

How to use routing in Vue to implement data transfer and state management between pages? How to use routing in Vue to implement data transfer and state management between pages? Jul 21, 2023 am 08:18 AM

How to use routing in Vue to implement data transfer and state management between pages? In Vue, Router is one of the core plug-ins for switching between pages. In addition to page jumps, routing can also be used to implement data transfer and status management. This article will introduce how to use Vue's routing plug-in (VueRouter) to realize data transfer and status management between pages, and provide corresponding code examples. Basic use of routing VueRouter is the official routing plug-in of Vue.js. It can

See all articles