javascript page jump pass value

WBOY
Release: 2023-05-16 10:14:08
Original
436 people have browsed it

In front-end development, issues such as page jumps and value transfer are often involved. JavaScript is a widely used language that can be used to jump to pages and pass parameters. This article will introduce the methods of JavaScript page jump and value transfer, and provide some application examples.

1. JavaScript method to implement page jump

  1. window.location.href

window.location.href is used to load a new page . Through this method, you can jump to the specified page on the current page. For example, the following code can jump to the page designated as "newpage.html" on the current page:

window.location.href = "newpage.html";

Ongoing page While jumping, you can also pass parameters to the new page. For example:

window.location.href = "newpage.html?username=Tom&age=20";

  1. window.location.replace

another One way to implement page jumps is to use window.location.replace. The function of this method is to replace the current page with a new page. For example, the following code will be replaced by the page specified as "newpage.html" on the current page:

window.location.replace("newpage.html");

For this In terms of methods, parameters cannot be passed while jumping to the page.

  1. window.open

window.open allows opening a specified web page in a new browser window. For example, the following code will open a page specified as "newpage.html" in a new window:

window.open("newpage.html");

Similarly, Parameters can also be passed through this method. For example:

window.open("newpage.html?username=Tom&age=20");

2. JavaScript page parameter passing method

  1. URL passing Parameter

URL parameter passing is a simple and easy-to-use method for page parameter passing. It passes the parameters to the new page as parameters in the URL. For example:

window.location.href = "newpage.html?username=Tom&age=20";

In the new page, you can use the URLSearchParams object in JavaScript to get the parameters in the URL. For example:

//Get the parameters in the URL
const searchParams = new URLSearchParams(window.location.search);

//Get the username
const username = searchParams. get('username');

//Get age
const age = searchParams.get('age');

  1. sessionStorage

sessionStorage is a Web storage solution provided by HTML5. It is similar to localStorage, but the data stored is session level, and the data will be cleared when the session ends. You can use sessionStorage to pass data between pages. For example, set the passed parameters in the previous page:

//Set the passed parameters
sessionStorage.setItem('username', 'Tom');
sessionStorage.setItem('age' , 20);

In the latter page, you can get the passed parameters through sessionStorage:

//Get the passed parameters
const username = sessionStorage.getItem('username') ;
const age = sessionStorage.getItem('age');

  1. localStorage

localStorage is also a Web storage solution provided by HTML5. Different from sessionStorage, The data stored in localStorage is permanent and will not be cleared even if the page or browser is closed. You can use localStorage to pass data between pages. For example, set the passed parameters in the previous page:

//Set the passed parameters
localStorage.setItem('username', 'Tom');
localStorage.setItem('age' , 20);

In the latter page, you can get the passed parameters through localStorage:

//Get the passed parameters
const username = localStorage.getItem('username') ;
const age = localStorage.getItem('age');

3. Application Example

The following is a practical application example to implement a page jump containing a form, and Pass the data from the form to the next page.

  1. Page one (index.html)



  
页面一  
Copy after login


Copy after login


  1. Page Two (pageTwo.html)



  
页面二  
Copy after login


用户名:

密码:

Copy after login


In page one, when the submit button is clicked, the submitForm method will be executed to splice the data in the form into one parameter and Passed to page two. In page two, the URL parameters will be obtained through the getSearchParams method and displayed on the page.

Summary

This article introduces the methods of JavaScript page jump and value transfer, and provides some application examples to help readers better understand and master these technologies. In actual development, appropriate methods should be selected according to the situation to make page jumps and value transfers simpler, more efficient, and safer.

The above is the detailed content of javascript page jump pass value. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!