当前位置:网站首页>Get parameters in URL
Get parameters in URL
2022-06-25 21:09:00 【Mr. Liu works hard】
Encapsulate a method
We know it well url The parameters in are displayed in this form
?id=123&name=tom&age=13…
The first one is : Basic usage Split with string
therefore . Come on, show it
function getParams(key) {
var result = {} // Define a global object
var str = window.location.search;
if(str.startsWith('?')) { // Judge str With ? At the beginning
var strParams = str.split('?')[1];
var arrParams = strParams.split('&');
// Then proceed for loop , It's directly used here forEach
arrParams.forEach((item) => {
var temKey = item.split('=')[0];
var temVal = item.split('=')[1];
result[temKey] = temVal
})
}
return result[key]
}
perhaps
getUrlParameter: function(field) {
// obtain Url Parameters
var GET = {};
var canshu=window.location.hash.split("?")[1]==undefined?'':window.location.hash.split("?")[1];
if(canshu!=''){
var strarr = canshu.split("&");
for(i=0;i<strarr.length;i++){
var xx = strarr[i];
var mykey = xx.split('=')[0];
var myvalue = xx.split('=')[1];
GET[mykey] = myvalue;
}
}
return GET[field];
}
The second kind :URLSearchParams
The method is JS Built in objects for , Can be mapped out search Query criteria
Use get Method , You can get the specified parameters
function get ParamsNew(key) {
var temData = new URLSearchParams(window.location.search);
return temData.get(key)
}

end
expand
When we make data changes , Suppose we want to get ID To show
var id = getParamsNew('id');
if(id){
// Yes id The data must be loaded when
loadData(); // Fill in the form with the data to be modified
};
// Method
function loadData() {
var strPeople = localStorage.getItem('myStudent'); // We from localStorage The value of
var people = [];
if(strPerple) {
people= JSON.parse(strPeople);
}
// If the arrow function has only one line , Don't write return
// We use... In the array find Method , Find the data of the review condition
var person = people.find((item) =>{
item.id == id // Find items that match the criteria
})
// Perform assignment operation
XXname.value = person.name;
XXage.value = person.age;
.....// So the assignment is successful
}
In this way, you can edit and display , However, you should pay attention to whether it is a combination of addition and modification , If you are in a piece, you need to add judgment conditions when saving
You can read this article https://blog.csdn.net/lzfengquan/article/details/118605300?spm=1001.2014.3001.5501
边栏推荐
- Section 13: simplify your code with Lombok
- Is it legal to open an account for flush stock trading software? Is it safe?
- How does zhiting home cloud and home assistant access homekit respectively? What is the difference between them?
- How to buy the millions of medical insurance for children? How much is it a year? Which product is the best?
- Svn various color states
- Is it safe for qiniu school to open an account in 2022?
- Bank digital transformation layout in the beginning of the year, 6 challenges faced by financial level structure and Countermeasures
- Differences between modems and routers (powercert animated videos)
- Illustrated with pictures and texts, 700 pages of machine learning notes are popular! Worth learning
- JS__ Inheritance mode, namespace, object enumeration__ Duyi
猜你喜欢

Flexible scale out: from file system to distributed file system
Yunzhisheng atlas supercomputing platform: computing acceleration practice based on fluid + alluxio (Part I)
The live registration is hot to start | the first show of Apache dolphin scheduler meetup in 2022!
Interviewer: why does TCP shake hands three times and break up four times? Most people can't answer!

A simple file searcher

js(3)
Uncover n core 'black magic' of Presto + alluxio
2022 "gold, silver and four" is a must for job hopping. You must know 100 questions in 2022 intermediate and advanced Android interview to realize your big factory dream
Understand the offline mixing technology in the industry

Lantern Festival, learning at the right time! Novice training camp attacks again, learning buff continues to fill up
随机推荐
[machine learning] machine learning from zero to mastery -- teaching you how to use decision tree to predict contact lens types
2022 oceanbase technical essay contest officially opened | come and release your force
股市小白通过网上进行股票开户安全吗?
Yolov4 reading notes (with mind map)! YOLOv4: Optimal Speed and Accuracy of Object Detection
Lantern Festival, learning at the right time! Novice training camp attacks again, learning buff continues to fill up
The correct way to clear the cache of the computer. The computer will not get stuck immediately after changing. practical
Introduction to event flow, event capture, and event bubbling
UDP Vs TCP (Powercert animated videos)
What are cookies in Web site development?
Cloud native 04: use envoy + open policy agent as the pre agent
JS__ Prototype, prototype chain, call/apply__ Duyi
04 disk space management
Must see the summary! In depth learning era, you should read 10 articles to understand image classification!
Soft test intermediate simple notes
OSI notes sorting
ThreadLocal class
Bank digital transformation layout in the beginning of the year, 6 challenges faced by financial level structure and Countermeasures
Uncover n core 'black magic' of Presto + alluxio
Cvpr2021 collection of papers | cvpr2021 latest papers | cvpr2021 review results | cvpr2021 admission results
Desktop network error display red ×, Component failed to start